Java garbage collection mechanism

Source: Internet
Author: User

JVM Frame

Framework for Java Virtual machine hotspot:

The main components of the JVM are Class Loader (ClassLoader), runtime data area (runtime), and execution engine (execution engine).

JVM Introduction to garbage collection

Step 1:marking

The GC algorithm usually needs to scan the surviving objects from the root node, scanning all surviving object references, and building an object graph.

Root: Static field, method parameter, local variable, CPU register

Step 2:normal Deletion

Removes objects that are not referenced and frees up space.

Step 2a:deletion with compacting

Removing garbage and compressing the surviving reference objects can help increase the efficiency of memory allocation.

Generational Garbage Collection (generation of garbage collection)

Hotspot Heap Structure:

Young Generation: Assign a new object from the Eden area, once the Eden area is full, a minor garbage collection occurs, moving the surviving objects in the Eden area and one survivor area to another survivor area, The surviving object, age plus 1, is promoted to old Generation when the surviving object reaches a threshold.

Old Generation: A place where long-lived objects are saved, major garbage collection occurs when old Generation is full.

Stop the World Event:minor garbage collection and major garbage collection are all stop the world Event, that is, when garbage collection pauses the execution of the thread in the program.

Permanent generation: Saves the metadata information that is used in the JVM to describe classes and methods.

Process summary diagram for the entire GC:

The basic hypothesis of GC generation is that the life cycle of most objects is very short and the survival time is short.

Assigning small objects is less expensive than creating them.

The GC prefers this small, short-lived object.

Make the object's life cycle as short as possible, such as in the method body, so that it can be reclaimed as soon as possible in the YOUNGGC, and not be promoted (romote) to the older generation (old Generation).

Object Assignment Optimization: Try to avoid the allocation of large objects, when the object is large enough to Eden generation, the JVM can only try to go to the old generation allocation, this situation needs to be avoided as soon as possible, because once assigned in the old generation, This object can only be recycled by the old generation GC or FULLGC.

Immutable objects can reduce the pressure on the GC: the Hotspot JVM, in order to improve the performance of YOUNGGC, avoids every time YOUNGGC scans the object references in old generation, using the card table method. Simply put, when an object in an old generation creates a new reference relationship to an object in young generation or releases a reference, it is marked dirty (dirty) on the token in the card table, and YOUNGGC, You just need to scan these dirty items. A mutable object's reference relationship to other objects may change frequently, and it is possible to hold more and more references, especially containers, during the run. These will cause the corresponding card table entries to be frequently marked as dirty. The reference relationships of immutable objects are very stable, and they are not swept to their corresponding items when the card table is scanned.

Specifying the container initialization size can reduce the pressure on the GC: each time the container expands to allocate more space, the GC may increase in number of times.

Various references: Java.lang.ref.Reference has several subclasses for processing and GC-related references. There are several simple reference types for the JVM:

Strong Reference, the most common reference.

Weak Reference is reclaimed by GC when there is no strong reference to it.

Soft Reference is collected by GC only when it is near Oom.

Phantom Reference, used primarily to identify the timing of an object being GC, is typically used to do some cleanup work.

Garbage Collector (garbage collector)

The garbage collector used by HotSpot version 1.6 (pictured from the deep understanding of Java Virtual machines: JVM advanced effects and best practices), the two collectors in the diagram have lines between them, indicating that they can be used together):

1) Serial Generation serial collector

There are two main features of the serial collector: first, it uses only a single thread for garbage collection. Second, it is an exclusive garbage collection. When the JVM is running in client mode, it is the default garbage collector.

2) Serial old age serial collector

The old age serial collector uses the tag-compression algorithm. Like the new generation serial collector, it is also a serial, exclusive garbage collector. Because garbage collection in the old age usually takes longer than the new generation garbage collection, in applications with large heap space, the application is likely to pause for a few seconds or longer, once the old serial collector starts. Nonetheless, the old-time serial collector can be used in conjunction with a variety of new generation recyclers, and it can also act as a backup collector for the CMS collector. To enable the old-age serial collector, you can try the following parameters:-XX:+USESERIALGC: The new generation, the old generation use the serial collector.

3) Parallel collector

The Parnew parallel collector is a garbage collector that works in the new generation, and it simply multithreading the serial collector. It has the same recycling strategy, algorithms, and parameters as the serial collector. The parallel collector is also an exclusive collector, and the application is paused during the collection process. The parallel collector can be turned on using the parameter-XX:+USEPARNEWGC, which sets the new generation to use the parallel collectors, the old generation using the serial collector. -XX:+USECONCMARKSWEEPGC: The new generation uses the parallel collector, the older generation uses the cms+ serial collector.

4) Cenozoic Parallel recovery (Parallel scavenge) collector

The Cenozoic Parallel recovery collector is also a collector using the replication algorithm. On the surface, it is a multi-threaded, exclusive collector, just like a parallel collector. However, the parallel collection collector has an important feature: it is very concerned about the throughput of the system.
The Cenozoic Parallel collection collector can be enabled with the following parameters:
-XX:+USEPARALLELGC: The new generation uses the parallel collection collector, the old generation uses the serial collector.
-XX:+USEPARALLELOLDGC: The new Generation and the old age are all used in parallel recycling collectors.

5) Old-age parallel collection collector

The old-time parallel collection collector is also a multi-threaded concurrency collector. Like the new generation of parallel recovery collectors, it is also a collector that pays attention to throughput. Old age The Parallel collection collector uses the tag-compression algorithm, which is JDK1.6 after it is enabled. The use of-XX:+USEPARALLELOLDGC can be used in both Cenozoic and Laosheng generations to use the parallel collection of collectors, a pair of very focused garbage collector combinations, which can be considered in throughput-sensitive systems. The parameter-xx:parallelgcthreads can also be used to set the number of threads when garbage collection occurs.

6) CMS collector

Unlike the parallel collection collector, the CMS collector focuses on system downtime. CMS is the abbreviation for Concurrentmark Sweep, meaning that the concurrent tag cleanup, from the name can be learned, it uses the tag-clear algorithm, but also a multi-threaded garbage collector. When CMS works, the main steps are: initial tag, concurrency token, re-tagging, concurrency cleanup, and concurrency reset. Where initial markup and re-tagging are exclusive to system resources, concurrent tagging, concurrent purging, and concurrent resets can be performed with the user thread. Therefore, as a whole, CMS collection is not exclusive, and it can be garbage collected during application run-up.

7) G1 Collector (Garbagefirst)

The goal of the G1 collector is to act as a garbage collector for a server, so it is expected to outperform the CMS collector for throughput and pause control. Compared to the CMS collector, the G1 collector is based on the tag-compression algorithm. As a result, it does not produce space debris, and it is not necessary to do an exclusive defragmentation work once the collection is complete. The G1 Collector also allows for very precise pause control. It allows the developer to specify that the garbage collection time does not exceed N when the pause is M. Using the parameter-XX:+UNLOCKEXPERIMENTALVMOPTIONS–XX:+USEG1GC to enable the G1 collector, set the target pause time for the G1 collector:-xx:maxgcpausemills=20,-xx: gcpauseintervalmills=200.

Reference:

Http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

Http://coolshell.cn/articles/11541.html

Http://www.ibm.com/developerworks/cn/java/j-lo-JVMGarbageCollection/index.html

Java garbage collection mechanism

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.