JVM garbage collection mechanism and jvm garbage collection

Source: Internet
Author: User

JVM garbage collection mechanism and jvm garbage collection

Scope: areas to be recycled

Among the five JVM memory models, three do not require garbage collection: Program counters, JVM stacks, and local method stacks. Because their lifecycle is synchronized with the thread, the memory they occupy will be automatically released as the thread is destroyed,Therefore, only the method area and heap need to be GC..


Premise: how to determine whether an object is dead

All garbage collection algorithms face the same problem:Find out memory blocks inaccessible to applicationsTo release it. This is not reachable mainly because the application has no reference to the memory block. in JAVA, an object is reachable to the application: this object is referenced by the root (the root mainly refers to the static variable of the class, or the object that is active in all the thread stacks) or the object is referenced by another reachable object.

 

Reference Counting Algorithm

Reference count is the simplest and most direct method. This method adds a reference count to each object, which indicates how many references the current program has referenced this object, if the reference count of this object is 0, this object can be collected as the target object of the garbage collector.

Advantage: simple, direct, and no need to pause the entire application

Disadvantages: 1. the compiler must be used together to generate special commands for reference counting. 2. Circular references cannot be processed.

Therefore, this method is an early policy for garbage collection, which is rarely used now.Sun's JVM does not use the reference counting algorithm for garbage collection. It is based on the root search algorithm..

Root Search Algorithm

Using a series of objects named "GC Root" as the starting point, you can search down from these nodes and find the path that has passed through is referred to as Reference Chain ), when an object to GC Root is not connected with any reference chain, the object cannot be reached, the object is not available, and the garbage collector will recycle the memory occupied by it.

 

In java, GCRoot objects include the following objects:

A. referenced objects in the Java Virtual Machine stack (the local variable table in the stack frame.

B. Objects referenced by class static attributes in the method area.

C. Objects referenced by constants in the method area.

D. Reference objects of the local JNI method in the local method stack.

 

Useless classes:

(1). All instances of this class have been recycled, that is, the java heap does not have instance objects of this class.

(2) The Class Loader for loading the class has been recycled.

(3). the java. lang. Class Object corresponding to this Class is not referenced anywhere, and the method of this Class cannot be accessed anywhere through the reflection mechanism.

Four types of references

When collecting an object, GC determines whether there are references pointing to the object. There are four types of references in JAVA:

(1) Strong Reference)

Strong references are the most common references. If an object has a strong reference, the garbage collector will never recycle it. When the memory space is insufficient, the Java Virtual Machine would rather throw an OutOfMemoryError to terminate the program abnormally, and does not recycle strongly referenced objects at will to solve the problem of insufficient memory.

(2) Soft Reference)

If an object only has soft references, the memory space is sufficient and the garbage collector will not recycle it;If the memory space is insufficient, the memory of these objects will be recycled.. The object can be used by programs as long as the garbage collector does not recycle it.Soft references can be used to implement memory-sensitive high-speed cache.

For example, if an application needs to read a large number of local images and reads images from the hard disk each time, the performance will be seriously affected. However, if all the images are loaded into the memory, it may also cause memory overflow. In this case, soft reference can be used to solve this problem.

The design idea is: Use a HashMap to store the ing between the image path and the soft reference associated with the corresponding image object. When the memory is insufficient, JVM automatically recycles the space occupied by these cached image objects, effectively avoiding memory overflow.

Soft references can be used together with a ReferenceQueue. If the referenced objects are recycled by the garbage collector, the Java virtual machine adds this soft reference to the reference queue associated with it.

(3) Weak Reference)

The difference between weak references and soft references is that only objects with weak references have a shorter life cycle. When the Garbage Collector thread scans the memory area under its jurisdiction, once it finds only objects with weak references,No matter whether the current memory space is sufficient or not, the memory will be recycled.. However, since the garbage collector is a thread with a low priority, it may not soon find objects with weak references.

Weak references can be used together with a ReferenceQueue, the Java virtual machine adds this weak reference to the reference queue associated with it.

(3) Phantom Reference)

As the name implies, "virtual references" are just the same as virtual ones, which are different from other types of references. Virtual references do not determine the object lifecycle. If an object only holds a virtual reference, it is the same as no reference and may be recycled by the garbage collector at any time.

Virtual references are used to check whether objects have been deleted from the memory and track the activity of objects recycled by the garbage collector.. One difference between virtual and soft references and weak references is that:The Virtual Reference must be used together with the reference Queue (ReferenceQueue ).. When the garbage collector is preparing to recycle an object, if it finds that it has a virtual reference, it will add this virtual reference to the reference queue associated with it before it recycles the object's memory.

ReferenceQueue queue = new ReferenceQueue ();PhantomReference pr = new PhantomReference (object, queue);


Policy: Mark the garbage collection policy in JVM-clear Algorithm


Mark the clear collectorStop all jobsScan each active object from the root, mark the scanned object, and clear the unmarked objects after marking.

Advantages:

1. Solve the Problem of circular reference

2. the compiler is not required to execute additional commands.

Disadvantages:

1. Each active object must be scanned, And the collection has been paused for a long time.

2. The tag-clearing algorithm scans the hidden object objects from the root set. After the tag is complete, It scans the unlabeled objects in the whole space for recycling, as shown in.

The tag-clearing algorithm does not need to move objects and only processes non-surviving objects. It is extremely efficient when there are many surviving objects, however, because the tag-clearing algorithm directly recycles non-surviving objectsMemory fragmentation.

Copy Algorithm


The replication collector divides the memory into two equal-size spaces. At a certain time point, only one space is active. When the active space is full, GC copies the active objects to the unused space, and the inactive space becomes the active space.

Advantages:

1. Scan only the objects that can be reached. You do not need to scan all objects. This reduces the application suspension time.

Disadvantages:

1. Additional space consumption is required. At a certain time point, a piece of memory is always in the idle state.

2. Copying objects requires a certain amount of overhead.

 

The replication algorithm scans the root set and copies the surviving objects to a new space that has not been used. This algorithm is extremely efficient when there are few surviving objects in the space, however, the cost is that a memory swap space is required for moving objects.

Tag-Sorting Algorithm


The tag sorting collector draws on the advantages of tag clearing and replication collectors. it is executed in two phases. In the first phase, it first scans all active objects and marks all active objects, in the second stage, unmarked objects are cleared first, and then active objects are copied to the bottom of the heap.

This algorithm greatly reduces memory fragments and does not require twice as much space as a replication algorithm.

The tag-sort algorithm uses the tag-clear algorithm to mark objects. However, when the object is cleared, all the surviving objects are moved to the left free space and the corresponding pointer is updated. The tag-sorting algorithm moves Objects Based on the tag-clearing algorithm. Therefore, the cost is higher, but the memory fragmentation problem is solved.

Generational collection Algorithm

The GenerationalCollecting algorithm is based on the garbage collection algorithm obtained after object lifecycle analysis.

As we have discussed earlier, the memory is mainly divided into three parts: New Generation, old generation, and persistent generation. Different three generations have different GC algorithms. The new generation is suitable for objects with short lifecycles and frequent creation and destruction. The old generation is suitable for objects with relatively long lifecycles, persistent substitution in Sun HotSpot refers to the method area (some JVMs do not have the permanent substitution Statement ). First, we will introduce the concepts and features of the next generation, the old generation, and the permanent generation.


Young (Young generation, New Generation ):One Heap in JVM specification is divided into three young generations. One Eden zone and two vor zones. Most objects are generated in the Eden area. When the Eden zone is full, the surviving objects will be copied to the primary vor zone (one of the two). When the primary vor zone is full, the surviving objects in this region will be copied to another region vor. When the region VOR is full, the surviving objects will be copied from the first region vor, the old generation will be copied. Note that the two regions of the same vor are symmetric and irrelevant. Therefore, objects copied from Eden and copied from the previous vor may exist in the same region, only objects copied from the first vor region are copied to the old district. In addition, there is always a blank vor area.

The new generation uses the replication algorithm and the tag-clear garbage collection algorithm.In the new generation, 98% of the objects are short-lived objects that die overnight. Therefore, the new generation does not need to be divided into two parts of memory with the same capacity, but into the Eden zone, vor from (same vor 0) and vor to (same vor1), which account for the default memory capacity of the new generation are, respectively. The region vor from and same vor to are blank, only Eden and one of them have a total of 90% of the new generation capacity used to allocate memory for newly created objects. only 10% of the New Generation vor memory is wasted. When the new generation memory space is insufficient, garbage collection is required, the surviving objects are copied to the blank vor memory area. Eden and non-blank vor are marked-cleaned and recycled. The two regions rotate.

If the blank volume vor space cannot store objects that are still alive, use the memory allocation guarantee mechanism to directly copy the new generation of objects that are still alive to the old generation memory. When creating large objects, if the new generation does not have enough continuous memory, the memory will be allocated directly in the old generation.

Java virtual machines are calledMinor GC, The number of times is frequent, and the recovery time is short.

Use Java Virtual Machine-XmnThe parameter can specify the new generation memory size.

 

Tenured (old generation, old generation): A portion of the Heap in JVMspecification stores objects that survive from the young generation. Generally, the old generation stores objects with long life periods.

Objects in the old generation are generally long-lived objects, and the survival rate of objects is relatively high.Use the tag-Sort garbage collection algorithm in the old generation.

Java virtual machine is calledMajorGC/Full GC, The number of times is relatively small, and the time for each recovery is also relatively long.

Java Virtual Machine-XmsThe parameter can specify the minimum memory size,-XmxThe parameter can specify the maximum memory size. The two parameters minus the new generation memory size specified by the Xmn parameter can calculate the minimum and maximum memory size of the old generation.

 

Perm (permanent and permanent ):The Method area in JVM specification is used to store static files. Currently, Java classes and methods are used. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as Hibernate, in this case, you need to set up a large persistent storage space to store the classes added during the running process.

The method area in the memory of the Java Virtual Machine is called permanent generation in the SunHotSpot Virtual Machine and is the memory area shared by various threads, it is used to store data such as class information, constants, static variables, and compiled code loaded by virtual machines.Permanent generation of garbage collection is relatively small, and the efficiency is relatively low,However, garbage collection is also required. Otherwise, an OutOfMemoryError error will still be thrown when the memory usage is insufficient permanently.

Permanent generation also uses the tag-Sorting Algorithm for garbage collection, Java Virtual Machine Parameters-XX: PermSizeAnd-XX: MaxPermSizeYou can set the initial size and maximum capacity of permanent generation.

Garbage collection process

We have read the JVM memory partition management above. Now we can see how the JVM garbage collection works.

First, when the J2EE application server is started, the JVM starts, and the JDK classes and interfaces are, the classes and interfaces required for running the application server, the Class and interface definition files of the J2EE application, and the compiled Class files or the Class files in the JAR package are loaded into the permanent storage zone of the JVM. Create a JVM in Eden, a JAVA object required for running the application server, and a JAVA object required for starting the J2EE application. After the J2EE application is started, it can provide external services.

JVM creates corresponding JAVA objects in the Eden region according to each request. When the Eden space is insufficient to create a new JAVA object, the JVM Garbage Collector executes garbage collection for the Eden region and destroys JAVA objects that are no longer referenced by other objects. (If this object is referenced by only one object without other objects, this object is also categorized as unnecessary and so forth), and moves the JAVA objects referenced by other objects to the survivor Zone 0.

If the survivor Zone 0 has sufficient space to store, it is directly stored in the survivor Zone 0; if the survivor Zone 0 does not have sufficient space to store, the JVM garbage collector performs garbage collection for the survivor Zone 0, destroy JAVA objects that are no longer referenced by other objects and move those referenced by other objects to survivor 1.

If the survivor area 1 has sufficient space to store, it is directly stored in the survivor area 1; if the survivor area 1 does not have sufficient space to store, the JVM garbage collector performs garbage collection for the survivor area 1, destroy JAVA objects that are no longer referenced by other objects and move those referenced by other objects to the elder care zone.

If the old-age care area has enough space to store, it will be directly placed in the old-age care area. If the old-age care area does not have enough space to store, the JVM garbage collector will perform garbage collection in the old-age care area, destroys JAVA objects that are no longer referenced by other objects and retains the JAVA objects referenced by other objects.

If no space exists in the last elder care zone, survivor Zone 1, survivor Zone 0, and Eden zone, the JVM will report "JVM heap space overflow (java. lang. outOfMemoryError: Java heap space) ", that is, there is no space in the heap space to create objects.

This is the JVM memory partition management, which is much faster than non-partition; In general, garbage collection is much faster; because it saves a lot of time without scanning the whole piece of memory when there is no need.

Object space allocation and promotion

(1) objects are preferentially allocated on Eden.

(2) large objects directly enter the Old Age

The virtual machine provides the-XX: PretenureSizeThreshold parameter. objects larger than this parameter value are directly allocated to the old age. Because the new generation uses the tag-copy policy, allocating large objects in Eden will result in a large number of memory copies between the Eden zone and the two zoovor zones.

(3) long-lived objects will enter the Old Age

Every time a MinorGC occurs in the region vor, the age increases by one year. When the age increases to a certain degree (15 years by default), the object is promoted to the old age.


Trigger: when to start GC

The trigger conditions for Minor GC (New Generation recovery) are relatively simple,When the Eden space is insufficient, Minor GC is started to recycle the new generation..

Full GC (recovery in the old age, usually accompanied by a MinorGC) has several triggering conditions:

(1) insufficient space in the old age

(2) Insufficient PermSpace

(3) the average size of MinorGC promoted to the old age is larger than the remaining space in the old age.

 

Note: PermSpace is not equivalent to the method area, but is only used by HotspotJVM to implement the method area using PermSpace. Some virtual machines use other mechanisms to implement the method area without PermSpace.


Implementation: Serial Collector)

A single thread executes the recycle operation. During the recycle period, all application threads are suspended. The default recycler in client Mode

 

Minor Collection ):Move the surviving objects in the Eden area To the To area. If the To area cannot be mounted, the objects in the From area can be moved directly To the old generation, in the From area, the age is greatly upgraded to the old generation. After the collection is complete, the Eden and From areas are empty. In this case, the functions of From and To are swapped. From is changed To, To is changed From, and To is empty before each round of collection. The design type is replication.

 

Full Collection ):The collection of the old generation can be divided into three steps: Mark, Sweep, and Compact ). The markup phase marks all surviving objects, clears the phase, and releases all dead objects. The merging phase merges all living objects into the previous part of the old generation, leave all idle fragments to the back. The design type is merged to reduce memory fragments.

Parallel Collector)

Multiple Threads are used for garbage collection at the same time. In a multi-core environment, CPU resources can be fully utilized to reduce the collection time and increase JVM productivity. The default recycler in Server mode is used. As with the serial recycler, the execution of all application threads is suspended during the recycle process.

 

Minor Collection ):Multiple Threads are used to collect garbage. The algorithm of each thread is the same as that of the serial recycler.

 

Full Collection ):The old generation is still single-threaded, the same as the serial recycler.

Parallel Compacting Collection)

The recycling of the young and old generations is done with multiple threads. Compared with the parallel recycler, the collection time of the old generation is shorter, thus reducing the Pause time ).

 

Minor Collection ):Same as ParallelCollector

 

Full Collection ):The old generation consists of three steps: marking, statistics, and merging. Here we use the idea of dividing the old generation into multiple fixed-size zones (region ). Mark the stage and divide all the surviving objects into N groups (which should be the same as the number of recycled threads). Each thread is independently responsible for its own group, mark the location of the surviving object and the survival rate of the Region (Region), and mark it as parallel. In the statistical phase, the survival rate of each Region (Region) is counted. In principle, the previous survival rate is relatively high, find the start location that is worth merging (the vast majority of objects are not worth merging), and the statistical phase is serial (single thread ). In the merge phase, based on the information in the statistical phase, multiple threads concurrently copy the surviving objects from one Region to another ).

Concurrent Mark clearing recycler (Concurrent Mark-Sweep Collector)

Also known as Low-latency collector (Low-latencyCollector), the application can be suspended for the shortest time through various means. The collection operation is basically performed concurrently with the application without merging or copying operations.

 

Minor Collection ):Same as ParallelCollector

 

Full Collection ):There are four steps: Initial Mark, ConcurrentMark, Remark, and Concurrent Sweep ). Note that there will be fragments because there is no merge operation.

Initialization phase: Pause the application thread to find all the surviving objects. It takes a short time and the recycler uses a single thread.

Concurrent mark stage: the recycler marks the operation and concurrent operation of the application. The recycler uses a single thread to mark the surviving object.

Re-MARK: because the application is running in the concurrent mark stage, objects may be added or modified during this process. So pause the application thread again, find all the modified objects, and use the multi-threaded flag.

Concurrent cleanup: the recycler cleanup operation and concurrent application running. The recycler uses a single thread to clear dead objects.

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.