preface

Abba Abba was asked to talk about her understanding of the JVM.

Come home and wait for notice

Interviewer: Do you know anything about the MEMORY model of the JVM? Can you tell us more about it?

Abba Abba: During the execution of JAVA programs, the JAVA VIRTUAL machine divides all the memory it manages into different data areas, which have their own purposes.

According to the JAVA Virtual Machine Specification SE 7 edition, the memory managed by the JAVA Virtual Machine consists of the following run-time data areas: heap, method area, virtual machine stack, local method stack, and program counter.

Interviewer: Can you give me an overview of these areas?

Abba abba: The method area mainly stores the class information, constants and static variables loaded by virtual machines. The heap area mainly stores objects. The virtual machine stack is used to store stack frames generated when the method is running, and the local method stack is used to store stack frames generated when the native method is running. A program counter is a place where the address of the cell in which the next instruction resides is stored.

Interviewer: Good. Do you know when the stack will overflow?

If the stack depth of the thread is greater than the maximum depth of the virtual machine running, StackOverflowError will be thrown, usually in recursive method calls.

Interviewer: Are there any other situations where stack memory runs out?

Abba Abba: There seems to be no other case……

Interviewer: Can you elaborate on the generation of stacks?

Abba Abba: The heap of words mainly carried out a generation, divided into new generation, old age, lasting generation.

Abba Abba: The new generation mainly stores some dead objects, while the old generation stores relatively stable objects or large objects. The persistent generation is used to store static files, and now Java classes, methods and so on.

Interviewer: Ok, that’s all for today’s interview. You go back and wait for the notice. 😈

Abba Abba: Ok.

Take the offer on the spot

Abba Abba: Do you know the memory model of the JVM? Can you tell us more about it?

Abba Abba: During the execution of JAVA programs, the JAVA VIRTUAL machine divides all the memory it manages into different data areas, which have their own purposes.

According to the JAVA Virtual Machine Specification SE 7 edition, the memory managed by the JAVA Virtual Machine consists of the following run-time data areas: heap, method area, virtual machine stack, local method stack, and program counter.

Interviewer: Can you give me an overview of these areas?

Abba abba: The method area mainly stores the class information, constants and static variables loaded by virtual machines. The heap area mainly stores objects. The virtual machine stack is used to store stack frames generated when the method is running, and the local method stack is used to store stack frames generated when the native method is running. A program counter is a place where the address of the cell in which the next instruction resides is stored.

Interviewer: Good. Do you know when the stack will overflow?

Abba Abba: There are two cases. First, StackOverflowError is raised if the stack depth of the thread is greater than the maximum depth of the virtual machine running, usually in recursive method calls. There is another case: if you keep creating threads, so that each thread will occupy a certain amount of space, finally cause stack space insufficient, report OOM exception. The heap, method area, local method stack, and virtual machine stack are all areas of the JVM that are subject to OOM exceptions. The program counter is the only area that doesn’t generate OOM.

Interviewer: Sure, tell me more about the distribution of the heap.

Abba abba: the heap is mainly divided into the new generation, the old generation, and the persistent generation. This persistent generation was defined in JDK1.8 as heap memory, instead of using meta space, which is no longer in the JVM and uses local memory.

Abba Abba: The new generation mainly stores some dead objects, while the old generation stores relatively stable objects or large objects. The persistent generation is used to store static files, and now Java classes, methods and so on.

Abba Abba: Subdivisions of the Cenozoic era.

Ababa: The Cenozoic era is divided into Eden area and Survivor area, and survivor area is divided into two areas (S0 and S1), and their ratio is 8:1:1 as shown in the figure. If it is a large object, it will directly enter the old age. When GC occurs in Eden area, the surviving object will be transferred to Survivor area. When the age of the object in Survivor area reaches the set threshold (15 by default), it will be transferred to the old age area.

Interviewer: Which areas are shared by threads and which areas are private to threads?

Abba Abba: The method area and heap area are shared by threads. The virtual machine stack, local method stack, and program counter areas are thread private.

Interviewer: Since the heap is shared, so when new objects are created, memory space in the heap can be contested by multiple threads. How does the JVM ensure thread-safety in this area?

Abba Abba: Well, the JVM handles this in two ways. 1 uses pointer collisions, i.e. CAS to compete for memory space, and the JVM maintains a free list to quickly find free memory. The JVM also uses TLAB and Thread Local Allocation Buffer to allocate some memory in Eden area to threads. When creating new objects, the Thread’s private space will be allocated to the objects. This can reduce the extra cost of competition.

Interviewer: Are people assigned to the heap?

Abba Abba: Most objects are allocated on the heap, but there are special cases where objects can be allocated on the stack. If you want to allocate objects on the stack, you need to perform escape analysis.

Abba Abba: The basic behavior of escape analysis is to analyze the scope of an object. When an object is defined in a method, it is said to escape if the object is referenced outside the method or can be accessed by other threads.

, Lord, Lord, if the identified an object will not escape out of the way, we can put this memory allocation of objects on the stack, then the object’s memory space can be changes with the stack frame of life cycle, create die together, so that we can reduce the burden of the heap, improve the performance of GC, thus improve the operation efficiency of the program.

Abba Abba: But escape analysis is not enabled by default, because escape analysis requires a lot of calculation of the scope of the object to determine whether the object will escape, and this calculation will affect the performance, so the implementation of stack allocation is complicated, generally not used. Users can manually start escape analysis with the -xx: +DoEsapeAnalysis parameter.

Interviewer: Are you familiar with interior space?

Interviewer: If the maximum contiguous space available in the old generation is greater than the sum of all the objects in the new generation, then the Minor collection is considered safe. If it is less than, The JVM then looks at the Handle configuration HandlePromotionFailure setting to see if the guarantee failure is allowed. If HandlePromotionFailure=true, it continues to check if the maximum contiguous available space of the old age is greater than the average size of the objects promoted to the old age, and if so, a Minor GC is attempted, but this Minor GC is still risky; If less than or HandlePromotionFailure=false, do a Full GC instead.

Interviewer: Have you learned anything about GC algorithms and garbage collectors?

Abba Abba: Yes, GC algorithms are mainly divided into the following categories (didi… Interviewer: Why don’t you come back tomorrow for a second interview if you’ve passed the interview

Abba Abba: Good.

conclusion

Learn about the basic mechanics of one of the JVM models, including object memory allocation, thread privations, and shared-related issues.

Minibus then went on a jVM-related interview trip, so she went home and spent her days catching up on the JVM.

❤️/ Thanks for your support /

That is all the content of this sharing. I hope it will help you

Don’t forget to share, like and bookmark your favorite things

Welcome to the public number programmer bus, from byte, shrimp, zhaoyin three brothers, share programming experience, technical dry goods and career planning, help you to avoid detours into the factory.