Java Garbage Collection Learning notes

Source: Internet
Author: User
Tags compact

In general, to write Java code, you basically do not need to hear the concept of garbage collection. This is no, for me who has written more than 5 years of Java code, I have not experienced any experience that requires the use of garbage collection knowledge to solve the problem. However, I still urge myself to spend a few days systematically (and quite simply) learn the Java garbage collection mechanism. I think learning the Java garbage collection mechanism can have at least the following benefits:

    1. Direct Help for System tuning
    2. Add a conversation to a peer chat or next job interview
    3. To further the pursuit of technological excellence

(i) generation management of Java heap memory

Java garbage collection consumes CPU and memory resources and slows down as memory becomes larger, which can severely affect the performance of the system. At the same time, there is a phenomenon in the Java system: Most Java objects are "short-lived". Based on this, Java uses a generational approach to memory management and uses different garbage collection algorithms in different memory generations to manage the finer granularity of memory, minimizing the impact of garbage collection on the system itself.

    As shown, Java's heap space is divided into three regions, namely, the new Generation (young Generation), the old generation (older Generation) and the Permanent generation (Permanent Generation). The newly created object is first stored in the Cenozoic, and after several generations of garbage collection (copying back and forth between survivor 0 and Survivor 1), the surviving objects will be transferred to the old age. The new generation of garbage collection is very frequent, so that most "short-lived" objects will be cleaned up in time, and because the new generation of memory space is usually small, recovery speed is relatively fast. In the old age, the storage of the new generation has experienced a lot of garbage collected after the object is still alive, these objects are relatively small, and the old memory is generally very large, not easy to fill, so the old age of garbage collection frequency is much lower than the new generation, thus reducing the impact on system performance. The permanent generation mainly holds the data information of the Java class itself, and when the Java class is no longer used, it will be garbage collected. Developers often cannot predict the size of a permanent generation, causing the program to often "Java.lang.OutOfMemoryError:Permgen space" errors, so in Java 8, The Metaspace replaces the permanent generation with the JVM process native memory space. By default, Metaspace will use all available memory for the JVM process.  in the new generation of GC is called minor GC, in the old age of GC is called Major Gc,full GC simultaneously in the new generation and the old age. The movement of objects, such as those mentioned above between survivor 0 and Survivor 1, is often involved in the garbage collection process, resulting in the need to update the object references. To ensure the correctness of the reference update, Java pauses all other threads, which are known as "Stop-the-world", causing the system to halt globally. Stop-the-world has an impact on system performance, so one of the principles of garbage collection is to minimize "Stop-the-world" time.     showing the stop-the-world of different garbage collectors shows different levels of Stop-the-word for serial, parallel, and CMS collectors, even with the latest G1 collectors.   (ii) garbage collection algorithm the first garbage collection algorithm has a reference counting method, but it is not used in engineering because of its poor performance and the inability to reclaim circular reference objects. Current Java garbage collection is primarily based on the tag-purge (Mark-sweep) algorithm, which roughly consists of two steps:
    1. From the GC root object, all objects are tagged, and gc root includes local variables, static variables, and running thread objects.
    2. Clear off unmarked objects
The tag-purge algorithm is the basic principle of Java garbage collection, and on this basis, Java also provides several variant algorithms, including the tag-compression (mark-sweep-compact) algorithm and tag-copy (mark-copy).   tag cleanup algorithm (Mark Sweep)The principle of the markup cleanup algorithm, which is the two steps mentioned above, has the advantage of reducing stop-the-world time, and the disadvantage is that it can cause memory fragmentation, as shown in:    

Tag compression algorithm (Mark Sweep Compact)

To solve the memory fragmentation problem, the tag compression algorithm, as shown, presses the surviving objects collectively to one end of the memory after the memory is reclaimed. The compression process needs to update the reference to the object, as described earlier, which increases the system Stop-the-world time.

Tag replication algorithm (Mark copy)

The tag-copy algorithm is a relatively efficient algorithm because it does not involve the deletion of useless objects, only the objects that are tagged to survive are copied from one memory area to another. However, the tag replication algorithm does not apply to older generations where there are many surviving objects, because a large number of object copies can degrade system performance. Java is primarily used in the Cenozoic as a markup replication algorithm, which includes replication from the Eden zone to the survivor zone and between the two survivor regions.

   (c) garbage collector in Java there are mainly 4 garbage collectors, each of which uses different algorithms for different memory generations. Java will determine a default garbage collector based on the basic configuration of the current system, which you can view with the following commands: 
Java-xx:+printcommandlineflags-version

On the author's computer, the output is:

-xx:+ useparallelgc"1.8.0_45"1.8.0_45-64-bit Server VM (build 25.45-b02, Mixed mode)

As you can see from the Red section, the parallel collector is used by default, which is the default garbage collector for most Java machines (especially servers).

Serial collector (Serial Collector)

as the name implies, the serial collector refers to the use of single-threaded garbage collection, the recovery will lead to a long stop-the-world, mainly used for stand-alone programs. In the new generation, the collector adopts the replication algorithm, and the tag-compression algorithm is used in the old age. The collector can be activated with the-XX:+USESERIALGC command-line option.   Parallel collector (Parallel Collector)The collector also uses the replication algorithm in the new generation, using the tag-compression algorithm in the old age, only uses the multi-threading method to carry on the garbage collection, thus greatly improves the recovery efficiency, but also needs stop-the-world in the recycling process. The collector can be activated through-XX:+USEPARALLELGC. In most cases, the parallel collector is the default collector for Java.   Concurrent Tag Purge collector (Concurrent Mark Sweep collector,cms)The collector uses a replication algorithm in the new generation, using the tag-purge algorithm (not tag-compression) in the old age. It is called "concurrency" because at some stage of the recycling process, the recycle thread executes concurrently with the user thread, not the whole recycling process can be parallel to the user thread, the collector also exists Stop-the-world, It's just that the stop-the-world lasts a little longer compared to other collectors. The collector can be activated through-XX:+USECONCMARKSWEEPGC.   G1 Collector (Garbage first Collector)The G1 Collector is the newest collector in the Java world, and in Java 9 it becomes the default garbage collector. The collector uses different ways to treat Java to memory than the collector mentioned above, as shown in. The collector can be activated through-XX:+USEG1GC.    

Java Garbage Collection Learning notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.