Interviewer : I still remember the last time you mentioned the JVM memory structure (runtime data area) and mentioned "heap", and then you said it was divided into several areas.
Interviewer : At that time I felt that if I continued, I might have to work overtime
Interviewer : is a bit empty today, let’s continue to talk about the "pile" piece
candidate : Well, I mentioned earlier that the "new generation" and the "old generation" are stacked. The "new generation" is divided into "Eden" and "Survivor" areas, and the "survivor" area is divided into "From Survivor" And the "To Survivor" area
candidate : Having said that, I want to talk about Java's garbage collection mechanism
Interviewer : Then you start your performance
Candidate : When we use Java, many objects are created, but we have not "manually" cleared these objects
candidate : If you use the C/C++ language, you need to free it when you run out.
candidate : Then why don't we manually release the "garbage" when writing Java? The reason is simple, JVM does it for us (automatic garbage collection)
Interviewer : Well...
candidate : My personal definition of garbage: As long as the object is no longer used, then we think that the object is garbage, and the space occupied by the object can be recycled
Interviewer : How do you judge that the object is no longer used?
Candidate : Commonly used algorithms have two "reference counting method" and "reachability analysis method"
Candidate : The idea of reference counting is very simple: when an object is referenced, it is +1, but if the object reference fails, it is -1. When the counter is 0, the object is no longer referenced and can be recycled
candidate : The most obvious disadvantage of the reference counting method is: if the object has a circular dependency, it is impossible to locate whether the object should be recycled (A depends on B, B depends on A)
Interviewer : Well...
candidate : The other is the reachability analysis method: it starts from "GC Roots" and searches downwards. When there is no reference to the object to "GC Roots", it means that the object is unusable and can be recycled
candidate : "GC Roots" is a set of references that must be "active". Starting from "GC Root", the program can find objects that may be being used by direct or indirect reference
Interviewer : still doesn't understand, what are "GC Roots" in general? You said it is a set of active references, can you give an example, it's too abstract.
candidate : For example, didn't we talk about the virtual machine stack in the JVM memory structure last time? Isn't there a stack frame in the virtual machine stack? Does the stack frame have local variables? Local variables don't just store references.
candidate : If the stack frame is at the top of the virtual machine stack, does it mean that the stack frame is active (in other words, the thread is being called)
candidate : Since it is being called by a thread, does the object reference pointing to the "heap" in the stack frame must be an "active" reference?
candidate : Therefore, the current active stack frame points to the object reference in the heap can be "GC Roots"
Interviewer : Well...
Candidate : Of course, it is not only the small piece above that can be used as "GC Roots"
candidate : For example, the static variable reference of the class is "GC Roots", the object referenced by the "Java Native Method" is also "GC Roots", etc...
candidate : Back to the point of understanding: "GC Roots" is a set of "references" that must be "active". As long as there is no direct or indirect reference to "GC Roots", it is garbage
candidate : JVM uses the "reachability analysis algorithm" to determine whether the object is garbage
Interviewer : Understood
candidate : The first step of garbage collection is to "mark", marking objects that are not referenced by "GC Roots"
candidate : After marking, we can choose to "clear" directly, as long as it is not associated with "GC Roots", we can kill
candidate : The process is very simple and rude, but there are also obvious problems
Candidate : Direct clearing will cause "memory fragmentation" problem: Maybe I have 10M free memory, but the program requests 9M memory space but can't apply (10M memory space is after the garbage is cleared, not continuous)
candidate : Solving the problem of "memory fragmentation" is relatively simple and rude. After "marking", do not directly "clear".
candidate : I "copy" the surviving object of the "mark" to another space, and after the copy is over, I directly kill the entire original space! This way there is no problem of memory fragmentation
candidate : The disadvantage of this approach is obvious: the memory utilization is low, and a new area must be copied (moved) for me.
Interviewer : Well...
Candidate : There is also a "compromise" method. I don’t need a "large complete space" to solve the problem of memory fragmentation, as long as I can move in the "current area"
candidate : Move the surviving objects to one side, move the garbage to the side, then delete the garbage together, there will be no memory fragments.
candidate : This kind of professional term is called "organization"
candidate : After so long, let's return our thinking to the "heap" again.
candidate : Research shows that most objects have a short life cycle, and only a few objects may survive for a long time
candidate : Also due to "garbage collection" will cause "stop the world" (application stop access)
candidate : It should be very simple to understand "stop the world": when garbage is collected, the program has a short time and cannot continue to operate normally. Otherwise, when the JVM is reclaiming, the user thread will continue to allocate and modify the reference, how does the JVM do it (:
candidate : In order to make the duration of "stop the world" as short as possible and increase the memory allocation rate that concurrent GC can handle
Candidate : In many garbage collectors, these two types of objects are distinguished "physically" or "logically". The area occupied by objects that die quickly is called the "young generation", and those who live long The area occupied by the subject is called the "old age"
candidate : But not all "garbage collectors" will have it, but we may use JDK8 online now. The garbage collectors used in JDK8 and below all have the concept of "generations" .
candidate : So, you can see that my "heap" is painted with "young generation" and "old generation"
candidate : It should be noted that the ZGC of the garbage collector used by the higher version does not have the concept of generation (:
Candidate : Just to explain the status quo, we will talk about ZGC when we have time
Interviewer : Well... okay
candidate : I mentioned the garbage collection process earlier, which actually corresponds to several "garbage collection algorithms", namely:
candidate : mark removal algorithm, mark copy algorithm and mark sorting algorithm ["mark" "clear" "copy" "organize"]
candidate : After the above preparation, these algorithms should be relatively easy to understand
candidate : After understanding the "generational" and "garbage collection algorithms", we can look at the common garbage collectors in the JDK8 production environment and below
Candidate : The "young generation" garbage collectors are: Seria, Parallel Scavenge, ParNew
Candidate : The garbage collectors of the "old age" are: Serial Old, Parallel Old, CMS
candidate : Seeing that there are many garbage collectors, it is actually very easy to understand. Serial is single-threaded, Parallel is multi-threaded
Candidate : These garbage collectors actually "implement" the garbage collection algorithm (mark copy, mark sorting, and mark clearing algorithms)
candidate : CMS is "before JDK8" is a relatively new garbage collector, its characteristic is to minimize the "stop the world" time. Allow user threads and GC threads to execute concurrently during garbage collection!
candidate : What can be found is that the "young generation" garbage collector uses the "mark copy algorithm"
candidate : So in the "heap memory" division, the young generation is divided into Survivor area (Survivor From and Survivor To), the purpose is to have a complete memory space for the garbage collector to copy (move)
candidate : and the new object is placed in the Eden area
candidate : I will re-draw the "heap memory" diagram below, because their sizes have a default ratio
candidate : I have already drawn the picture, so I don’t need to explain it anymore.
Interviewer : I also want to ask, that is, newly created objects are generally in the "new generation", when will they be in the "old generation"?
candidate : Well, I think the simplicity can be divided into two situations:
candidate : 1. If the object is too large, it will directly enter the old age (the object is very large when it is created || Survivor area cannot save the object)
candidate : 2. If the object is too old, it will be promoted to the old age (every time a Minor GC occurs, the age of the surviving object is +1, and the default value 15 is promoted to the old age || Dynamic object age determination can enter Old age)
Interviewer : Since you mentioned Minor GC again, when will Minor GC be triggered?
candidate : When the Eden area is insufficient, the Minor GC will be triggered
Interviewer : In my understanding, Minor GC is the "young generation" GC. You mentioned "GC Roots" again.
Interviewer : In the "young generation" GC, starting from GC Roots, wouldn't it also scan the "old generation" objects? So, that... isn't it equivalent to a full heap scan?
candidate : There is also a solution in this JVM.
candidate : HotSpot virtual machine "old GC" (below G1) requires the entire GC heap to be in a contiguous address space.
candidate : So there will be a dividing line (one side is the old generation, the other side is the young generation), so you can judge which generation of the object is based on the "address"
Candidate : As Minor GC, starting from GC Roots, if you find objects in the "old age", then don't go down (Minor GC has no interest in the area of the old age)
Interviewer : But there is another question, what if the "young generation" object is quoted by the "old generation"? (The old generation object holds a reference to the young generation object). At that time, of the "young generation" object must not be recycled.
candidate : There is a "card table" under the HotSpot virtual machine to avoid global scanning of "old generation" objects
candidate : Each small area of the "heap memory" forms a "card page", and the card table is actually a collection of card pages. When judging that there is a cross-generational reference of an object in a card page, mark this page as a "dirty page"
candidate : After knowing the "card list", it is easy to handle. Every time Minor GC only needs to go to the "card table" to find the "dirty page", and add it to the GC Root after finding it, instead of traversing the entire "old age" object.
Interviewer don't you continue to talk about CMS?
Candidate : This interview is almost an hour, and I have drawn so many pictures. next time? Next time? A little tired
This article summarizes :
- What is garbage : As long as the object is no longer used, it is garbage
- How to judge : reachability analysis algorithm and reference calculation algorithm, JVM uses reachability analysis algorithm
- What is GC Roots : GC Roots is a set of references that must be active, and references that are not associated with GC Roots are garbage and can be recycled
- Common garbage collection algorithm : mark removal, mark copy, mark sorting
- need to be divided into ? Most objects die early, and only a few objects will survive for a long time. The heap memory will be physically or logically divided, in order to make the "stop the world" duration as short as possible and to increase the memory allocation rate that the concurrent GC can handle.
- Minor GC : trigger when the Eden area is full, traverse down from GC Roots, the young generation GC does not care about objects in the old generation
- What is the card table [Card table]: space for time (similar to bitmap), which can avoid scanning all correspondences of the old generation and proceed smoothly with Minor GC (Case: the old generation object holds a reference to the young generation object)
- heap memory ratio : The young generation occupies 1/3 of the heap memory, and the old generation occupies 2/3 of the heap memory. The Eden area accounts for 8/10 of the young generation, and the Survivor area accounts for 2/10 of the young generation (1/10 of each station of From and To)
Welcome to follow my WeChat public [16189c81c5e35e Java3y ] to talk about Java interviews. The online interviewer series is being updated continuously!
[Online Interviewer-Mobile] The series updated twice a week!
[Online Interviewer-Computer] The series two articles a week are being updated continuously!
Originality is not easy! ! Seek three links! !
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。