preface
After some time ago, a ba a ba was invited into the second interview, this time she and the interviewer will continue to embarrass her, to ask her about GC algorithm
Come home and wait for notice
Interviewer: Do you know anything about garbage collection for the JVM?
Abba Abba: Yeah, a little bit.
Interviewer: So how does the JVM determine that an object is garbage?
Abba Abba: There seems to be an accessibility analysis.
Abba Abba: Reachable objects are judged as live objects, and unreachable objects are treated as “garbage”.
Interviewer: Well…. Tell me what you know about garbage collection algorithms.
Abba abba:
Mark clear algorithm
Tag sorting algorithm
Replication algorithm
Generational collection algorithm
Interviewer: Well…. Do you know anything about these algorithms?
Abba Abba: Um…. Don’t know much about…
Interviewer: line, today first face to here, your side first return to wait for notice 😈
Abba Abba: Ok.
Unfortunately, you failed the interview, your resume has been added to the talent pool of the company, looking forward to meeting…… next time
Take the Offer on the spot
Interviewer: Do you know anything about garbage collection for the JVM?
Abba Abba: Yeah, a little bit.
Interviewer: So how does the JVM determine that an object is garbage?
Abba Abba: There are two methods, one is reference counting and the other is reachability analysis.
Abba abba: Object reference counting method is to give a reference counter, whenever there is a reference to the object, adds a reference counter and whenever there is a reference to disconnect the reference count is minus one, so that when the reference count is zero, then think that there’s no point in the object, the so-called junk, but this approach has a big, Cannot handle circular references.
Abba abba: The situation where circular references to objects have external references, which seems fine until we break the method references.
Abba abba:The condition in which an external reference to an object referenced by a loop breaks.
, Lord, Lord: this reference to disconnect, apparently object A and object B has no external references to refer to them, they have become A waste, and the reference counter because they refer to each other (circular references), its value is 1, lead to cannot be recycled, the defects lead to reference counting method is not used in the JVM.
Abba Abba: The method of reachability analysis is to search the object of GC Roots down from its root. This searched path is called “reference chain”. When an object is not linked by any GC Roots reference chain, then the object is judged to be “dead”, we generally call the object “unreachable”.
Interviewer: You just mentioned GC Roots. Do you know who can be used as GC Roots?
Abba Abba: Yeah, there are four main types of objects that can be used as objects for GC Roots.
Object referenced in the virtual machine stack
Objects referenced by static properties in the method area
The object referenced by the constant in the method area
Objects referenced in the local method stack
Abba Abba: The graph below shows the relationship.
Abba Abba: As you can see, only objects that are on the referenced chain are judged to be “alive”, while objects that are not on the referenced chain are judged to be “dead” and will also be recycled as garbage.
Interviewer: That’s a good point. Is there garbage collected in the method area in addition to the objects in the heap?
Abba Abba: The method area also has garbage collection, the method area mainly collects discarded constants and useless classes.
Interviewer: Well…. Tell me what you know about garbage collection algorithms.
Abba Abba: There are four main types of garbage collection algorithms.
Mark clear algorithm
Tag sorting algorithm
Replication algorithm
Generational collection algorithm
Abba ABba: Mark clear algorithm, is divided into two stages, the first stage for “mark”, the second stage for “clear”, first mark all the objects to clear, that is, the gray part, and then recycle.
Abba Abba: A lot of space debris is generated after the garbage cleaning of heap using the mark clear algorithm, which makes it difficult to allocate the memory of new objects. Moreover, the efficiency of the mark clear algorithm is not too high in the mark stage and the clean stage.
Abba abba:The token collation algorithm was born to solve the problem of excessive memory fragmentation.
, Lord, Lord, in order to solve the problem of efficiency, replication algorithm also appeared, which put a piece of memory is divided into equal to the size of the 2 pieces, each time you use to use only one piece, when a never used up, live in the memory object is transferred to another piece of memory, then the in-memory object all empty.
Abba abba:The replication algorithm is simple, convenient, and efficient, and does not need to worry about memory fragmentation, but it is expensive to reduce memory by half.
Abba abba: And most of the new generation of objects are dead, according to the 1:1 space ratio to use the copy algorithm, will greatly affect the performance of memory.
Abba Abba: The generation algorithm is to divide the heap and apply the corresponding garbage collection algorithm according to the situation of the different regions.
Ababa: The following figure is a detailed description of the Cenozoic era. The Cenozoic era is divided into Eden area and Survivor area. Survivor area is divided into two areas (S0 and S1), and their ratio is 8:1:1, as shown in the figure. New objects will be allocated in Eden area first, and the tag clearing algorithm is not applicable here, because there are too many fragments. If there is no continuous enough space to allocate objects, garbage collection will continue to be triggered, which has a great impact on performance.
Abba abba: For traditional GC, there is no way to avoid “stop-the-world” in THE GC process, which is generally referred to as STW. STW has a great influence on system performance, so how to eliminate STW or reduce THE time of STW is particularly important. In fact, generational algorithm is not a specific algorithm. Different from the previous mark clearing, mark sorting algorithm, and copy algorithm, the generation algorithm is just a division of the heap, and then use different algorithms in different areas, so as to subdivide the STW time into various areas, so that the STW time will not last for a long time, to achieve the purpose of improving the system performance.
Interviewer: speak very clear meticulous, very good, tomorrow come to work 😈.
conclusion
For garbage collection algorithms, be sure to talk about GC Roots, various garbage collection algorithms, and their advantages and disadvantages.
❤️/ 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.