In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what is the JVM garbage collector". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the JVM garbage collector"?
1. Briefly describe the mechanism of Java garbage collection.
In java, programmers do not need to display to free the memory of an object, but are executed by the virtual machine itself. In JVM, there is a garbage collection thread that is low-priority and does not normally execute, triggering execution only when the virtual machine is idle or the current heap is out of memory, scanning objects that are not referenced and adding them to the collection to be recycled.
2. What is GC? Why GC?
GC means garbage collection (Gabage Collection). Memory processing is where programmers are prone to problems, forgotten or wrong memory.
Recycling can lead to instability or even crash of the program or system. The GC function provided by Java can automatically monitor whether the object exceeds the scope to achieve automatic
For the purpose of reclaiming memory, the Java language does not provide a display operation to free allocated memory.
3. The advantages and principles of garbage collection. And consider two kinds of recovery mechanism.
The most prominent feature of Java language is the introduction of garbage collection mechanism, which makes java programmers no longer consider the problem of memory management when writing programs.
Because of this garbage collection mechanism, objects in java no longer have the concept of "scope", only referenced objects have "scope".
Garbage collection mechanism effectively prevents memory leaks and can effectively use available memory.
The garbage collector usually runs as a separate low-level thread that clears and collects objects in the memory heap that are dead or have not been used for a long time in unpredictable circumstances.
Programmers cannot call the garbage collector on an object or all objects in real time for garbage collection.
Garbage collection includes generation-by-generation replication garbage collection, marked garbage collection and incremental garbage collection.
4. What is the basic principle of the garbage collector? Can the garbage collector reclaim memory immediately? Is there any way to actively notify the virtual machine for garbage collection?
For GC, when a programmer creates an object, GC begins to monitor the address, size, and usage of the object.
In general, GC records and manages all objects in the heap (heap) in a directed graph. In this way, determine which objects are "reachable" and which are "unreachable." When GC determines that some objects are "unreachable", GC has the responsibility to reclaim this memory space.
Sure. Programmers can manually execute System.gc () to tell GC to run, but the Java language specification does not guarantee that GC will be executed.
5. What are the reference types in Java?
Strong reference: it will not be recycled when gc occurs.
Soft references: useful but not necessary objects that are recycled before a memory overflow occurs.
Weak references: useful but not necessary objects that will be recycled the next time you GC.
Virtual reference (ghost reference / phantom reference): you cannot get an object through a virtual reference, but use PhantomReference to implement a virtual reference, which is used to return a notification when gc.
6. How to judge whether the object can be recycled?
When the garbage collector is doing garbage collection, the first thing to decide is which memory needs to be reclaimed, which objects are "alive" and cannot be recycled, and which objects are "dead" and need to be recycled.
There are generally two ways to judge:
Reference counter method: create a reference count for each object, count + 1 when there is an object reference, count-1 when the reference is released, and be recycled when the counter is 0. It has a disadvantage that it can't solve the problem of circular reference.
Reachability analysis algorithm: search downward from GC Roots, and the path taken by the search is called reference chain. When an object does not have any reference chain to the GC Roots, it is proved that the object can be recycled.
7. When can an object be garbage collected in Java
When an object becomes unreachable to the application that currently uses it, the object can be recycled.
Garbage collection does not occur in the permanent generation, and if the permanent generation is full or exceeds the critical value, complete garbage collection (Full GC) will be triggered. If you take a closer look at the output of the garbage collector, you will find that Yongdai is also recycled. This is why the correct permanent generation size is so important to avoid Full GC.
8. Will garbage collection occur in the permanent generation in JVM
Garbage collection does not occur in the permanent generation, and if the permanent generation is full or exceeds the critical value, complete garbage collection (Full GC) will be triggered. If you take a closer look at the output of the garbage collector, you will find that Yongdai is also recycled. This is why the correct permanent generation size is so important to avoid Full GC. Please refer to Java8: from permanent generation to metadata area
(translator's note: permanent generation has been removed from Java8 and a new native memory area called metadata area has been added.)
9. What garbage collection algorithms does JVM have?
Mark-clear algorithm: Mark useless objects, and then clean up and recycle. Disadvantages: inefficient, unable to remove garbage fragments.
Replication algorithm: divide two memory areas of equal size according to capacity, copy the living objects to the other when one piece is used up, and then clean up the used memory space at once. Disadvantages: memory utilization is not high, only half of the original.
Tag-collation algorithm: Mark useless objects, move all living objects to one end, and then directly clear memory beyond the off-end boundary.
Generation algorithm: according to the different survival cycle of the object, the memory is divided into several blocks, generally the new generation and the old age, the new generation basically uses the replication algorithm, and the old age uses the tag finishing algorithm.
Mark-clear algorithm
Mark useless objects, and then clear and recycle.
The tag-cleanup algorithm (Mark-Sweep) is a common basic garbage collection algorithm that divides garbage collection into two phases:
Marking phase: Mark out objects that can be recycled.
Cleanup phase: reclaims the space occupied by the marked object.
The reason why the tag-removal algorithm is basic is that the garbage collection algorithms mentioned later are improved on the basis of this algorithm.
Advantages: it is easy to implement and does not need to move objects.
Disadvantages: the marking and cleaning process is inefficient, resulting in a large number of discontinuous memory fragments, which increases the frequency of garbage collection.
The execution process of the mark-clear algorithm is shown in the following figure
Replication algorithm
In order to solve the problem of low efficiency of mark-erase algorithm, a replication algorithm is produced. It divides the memory space into two equal areas, using only one area at a time. When garbage collection, traverse the currently used area, copy the surviving objects to another area, and finally recycle the recyclable objects in the currently used area.
Advantages: allocating memory sequentially is easy to implement and efficient, without considering memory fragmentation.
Disadvantages: the available memory size has been reduced to half of the original size, and objects will be copied frequently when the survival rate of objects is high.
The execution process of the replication algorithm is shown in the following figure
Marking-finishing algorithm
Replication algorithm can be used in the new generation, but in the old era, we can not choose the replication algorithm, because the survival rate of objects in the old era will be higher, so there will be more replication operations, resulting in lower efficiency. The mark-erase algorithm can be used in the old age, but it is not efficient and it is easy to produce a lot of memory fragments after memory recovery. So there is a tag-collation algorithm (Mark-Compact) algorithm. Unlike the tag-collation algorithm, after marking recyclable objects, all the surviving objects are compressed to one end of memory, so that they are compactly arranged together, and then the memory beyond the end boundary is reclaimed. After recycling, both used and unused memory are on their own side.
Advantages: the problem of memory fragmentation in the tag-cleaning algorithm is solved.
Disadvantages: local object movement is still needed, which reduces the efficiency to a certain extent.
The execution process of the tag-collation algorithm is shown in the following figure
Generation collection algorithm
At present, commercial virtual machines use generation-by-generation garbage collection algorithm. The generational collection algorithm, as its name implies, divides the memory into several blocks according to the survival cycle of the object. It generally includes the younger generation, the old generation and the permanent generation, as shown in the figure:
10. What kind of garbage collectors does JVM have?
If the garbage collection algorithm is the methodology of memory collection, then the garbage collector is the concrete implementation of memory collection. The following figure shows seven collectors for different generations, including Serial, PraNew, Parallel Scavenge for the new generation, Serial Old, Parallel Old, CMS for the old age, and G1 collector for the entire Java heap. The connections between different collectors indicate that they can be used together.
Serial collector (replication algorithm): the new generation of single-threaded collector, marking and cleaning are single-threaded, the advantage is simple and efficient
ParNew collector (replication algorithm): newborn generation parallel collector, which is actually a multithreaded version of Serial collector, has better performance than Serial in multi-core CPU environment.
Parallel Scavenge collector (replication algorithm): a new generation of parallel collector, the pursuit of high throughput, efficient use of CPU. Throughput = user thread time / (user thread time + GC thread time). High throughput can efficiently use CPU time to complete program tasks as soon as possible, which is suitable for scenarios such as background applications that do not require high interaction.
Serial Old collector (mark-collation algorithm): old-fashioned single-threaded collector, older version of Serial collector
Parallel Old collector (mark-collation algorithm): old-age parallel collector, throughput first, older version of Parallel Scavenge collector
CMS (Concurrent Mark Sweep) collector (mark-clear algorithm): the old parallel collector, which aims to obtain the shortest recovery pause time, has the characteristics of high concurrency and low pause, and pursues the shortest GC recovery pause time.
G1 (Garbage First) collector (mark-destruct algorithm): Java heap parallel collector. The G1 collector is a new collector provided by JDK1.7. The G1 collector is implemented based on the "mark-destruct" algorithm, that is, it does not produce memory fragmentation. In addition, an important feature of G1 collectors that is different from previous collectors is that the scope of G1 recovery is the entire Java heap (including the Cenozoic era and the old age), while the scope of the first six collectors is limited to the Cenozoic or the old era.
11. Introduce the CMS garbage collector in detail?
CMS, the abbreviation of Concurrent Mark-Sweep in English, is a garbage collector that obtains the shortest recovery pause time at the expense of throughput. This garbage collector is very suitable for applications that require server response speed. Add "- XX:+UseConcMarkSweepGC" to the parameter that starts JVM to specify the use of the CMS garbage collector.
CMS uses the mark-clear algorithm, so a large number of memory fragments will be generated during gc. When the remaining memory can not meet the requirements of the program, Concurrent Mode Failure will appear in the system, and the temporary CMS will use the Serial Old collector for garbage removal, and the performance will be degraded.
What are the new generation garbage collectors and the old garbage collectors? What's the difference?
New generation recyclers: Serial, ParNew, Parallel Scavenge
Old-age recyclers: Serial Old, Parallel Old, CMS
Whole heap recycler: G1
The new generation of garbage collector generally uses the replication algorithm, the advantage of the replication algorithm is high efficiency, the disadvantage is low memory utilization; the old collector generally uses the marking-finishing algorithm for garbage collection.
13. How does the generation-by-generation garbage collector work?
The generational recycler has two partitions: the old generation and the new generation, the default space proportion of the new generation is 1 amp 3 of the total space, and the default proportion of the old generation is 2 max 3.
The new generation uses the replication algorithm, and there are three partitions in the new generation: Eden, To Survivor, and From Survivor. Their default share is 8:1:1, and its execution process is as follows:
Put the living objects of Eden + From Survivor into the To Survivor area
Clear Eden and From Survivor partitions
From Survivor and To Survivor partition exchange, From Survivor changes from To Survivor,To Survivor to From Survivor.
An object that survives every time From Survivor moves to To Survivor is + 1, and when it reaches 15 (the default is 15), it is upgraded to the old generation. The big object will also go straight into the old age.
When the space occupancy reaches a certain value, the older generation will trigger global garbage collection, which generally uses the execution algorithm of tag collation. The above cycles constitute the overall implementation process of the whole generation of garbage collection.
At this point, I believe you have a deeper understanding of "what is the JVM garbage collector". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.