Garbage collection in the JVM

Source: Internet
Author: User

First, the concept of garbage collection

 Memory recycling in Java is a collection of memory that is run by the JVM, and the areas that need to be reclaimed are the method area and the Java heap. Because of program counters, the Java Virtual machine stack and the local method stack are automatically recycled at the end of the method or at the end of the thread, so there is no need to consider recycling. Why do we need garbage collection? Because the size of the memory in the computer is limited and fixed, in the process of the load and creation of the class, the memory used in memory will be more and more large, resulting in the subsequent execution of the program can not be allocated memory for execution, it is necessary to garbage collection, the area of memory already used is not in use of the data purged, So that later programs can run normally.

Second, how to judge an object can be recycled?

  at the time of garbage collection, how can you tell if an object is ready for recycling? When an object can be recycled, it is no longer a variable that references the object. General to determine whether an object also has a variable reference, using the reference counting method. is to add a reference counter to the object, and if a variable references the object, add one to the counter and subtract one when the reference fails. When garbage collection is done, the object with zero value inside the counter is recycled. However, the mainstream Java Virtual machine does not use this method for garbage collection, but uses the GC Roots accessibility algorithm to determine whether an object can be recycled.

When the GC Roots accessibility algorithm is used to determine whether an object is recycled, it is through a series of objects that can be used as GC roots as the starting point, searching down the object, forming multiple objects can be GC roots the path of the object, called the reference chain, when an object does not exist in the reference chain, it can be recycled.

  

Objects A, B, C, D in the diagram have paths (chain of reference) to the GC roots object, and F does not exist, so a memory reclaim reclaims object F without Reclaiming object a,b,c,d.

Third, garbage collection algorithm

  J memory Reclamation in a VM uses the following recovery algorithms:

1. Mark-Purge algorithm: When the garbage collection is done, the objects that are no longer referenced are marked, and when the tag is complete, the objects that are tagged are recycled uniformly.

When not recycled:

  

Mark-When cleared, all unreferenced objects are marked, and the memory of the tagged unreferenced objects is reclaimed after the tag is complete.

Mark-After clearing:

  

2. Marking-Sorting algorithm: When the garbage collection, the object is no longer referenced to mark, after the completion of the tag, after the unified collection of tagged objects, the memory is organized, the reference objects moved together

Mark-Before finishing:

  

As with the mark-and-clear method, unreferenced objects are marked, cleared after the mark is complete, and then moved to the memory of the Reference object.

After the tag has been collated:

3. Copy algorithm:: Divide the memory into equal two blocks of memory, allocate only one piece of memory at a time, when the space in memory is used, the surviving objects in memory will be copied to another unused memory, then the space of memory used will be cleaned up.

Before recycling:

After the replication algorithm is recycled:

  

4. Generational algorithm: The recovery is divided into the new generation and the old era, the new generation of the use of replication algorithm, the old age using marker-clear or marker-collation algorithm

Iv. garbage collector  

garbage Collector is the concrete implementation of garbage collection algorithm.    Several garbage collectors exist in the J VM

  

1.Serial Collector: Use a single thread for garbage collection, garbage collection must stop all the work process for garbage collection, using the tag-collation algorithm.

2..Serial old collector: The older version of the Serial collector.

3.ParNew Collector: Multi-threaded version of the Seria collector.

4.Parallel Scavenge Collector: A collector that uses a replication algorithm to control the throughput for garbage collection, the throughput is the ratio of the CPU running user code to the total CPU time, and users can control the efficiency of garbage collection by setting throughput.

5.Parallel old collector: The older version of the Parallel scavenge, using the tagging algorithm and using multi-threaded recycling.

6.CMS Collector: A collector that targets the shortest recovery pause time, using the mark-sweep algorithm. The process for recycling is: Concurrent cleanup, re-tagging, such as the initial tag, concurrency tag.

7.G1 Collector: The latest collector, the recycle process is: initial tag, concurrent tag, final tag, filter recycle. 

Garbage collection in the JVM

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.