Garbage collection in Jvm and Jvm garbage collection

Source: Internet
Author: User

Garbage collection in Jvm and Jvm garbage collection

I. Concept of garbage collection

Memory collection in Java is the memory collection during Jvm runtime. The areas to be recycled include the method area and Java heap. Because of program counters, Java Virtual Machine stacks and local method stacks are automatically recycled at the end of methods or threads, so you do not need to consider recycling. Why garbage collection? Because the memory size in the computer is limited and fixed, during the running process, due to the loading and creation of classes, the memory used will become larger and larger, as a result, the allocated memory cannot be used for execution during subsequent program execution. In this case, garbage collection is required to clear unused data in the used memory area, so that later programs can run normally.

2. How can I determine whether an object can be recycled?

  How can we determine whether an object can be recycled during garbage collection? When an object can be recycled, it is no longer referenced by variables. The reference counting method is used to determine whether an object has variable references. Add a reference counter to the object. If a variable references this object, add one to the value in the counter. If the reference is invalid, the value is reduced by one. When garbage collection is performed, the objects with zero values in the counter are recycled. However, mainstream Java virtual machines do not use this method for garbage collection. Instead, they use the GC Roots accessibility algorithm to determine whether an object can be recycled.

When GC Roots accessibility algorithms are used to determine whether an object is recycled, a series of objects that can be used as GC Roots as the starting point are searched down to form a path where multiple objects can be GC Roots objects, it is called a reference chain. When an object does not exist in this reference chain, it can be recycled.

  

The objects A, B, C, and D in the figure all have paths (reference chains) that reach the GC Roots object, and F does not exist. Therefore, the object F will be recycled when memory is recycled, instead of recycling objects A, B, C, and D.

Iii. Garbage collection Algorithm

Memory reclaim in Jvm uses the following collection algorithms:

1. Tag-clearing algorithm: When garbage collection is performed, objects that are no longer referenced are marked. After the marking is completed, all marked objects are recycled.

When not recycled:

  

 

When the tag is cleared, all unreferenced objects are marked. After the tag is complete, the memory of the marked unreferenced objects is recycled.

Mark-after clearing:

  

2. tag-sorting algorithm: When garbage collection is performed, objects that are no longer referenced are marked. After the marking is completed, the marked objects are recycled and the memory is sorted, move referenced objects together

Mark-Before sorting:

  

Like the Mark-clear method, the unreferenced object is marked, cleared after the mark is complete, and the memory of the referenced object is moved together.

After marking:

3. replication algorithm: divides the memory into two equal memories, which are allocated with only one of them at a time. When the space in the memory is used up, copy the surviving objects in the used memory to another unused memory, and then clear the occupied memory space.

Before recycling:

After the replication algorithm is recycled:

  

4. Generational algorithm: This algorithm divides the collection into the new generation and the old generation. The new generation uses the replication algorithm, and the old generation uses the tag-clear or tag-sort algorithm.

Iv. Garbage Collector  

The garbage collector is the specific implementation of the garbage collection algorithm. Several garbage collectors exist in Jvm.

  

1. Serial COLLECTOR: a single thread is used for garbage collection. During garbage collection, all worker processes must be stopped for garbage collection and the tag-sorting algorithm is used.

2. Serial Old COLLECTOR: the Old version of Serial collector.

3. ParNew COLLECTOR: multi-threaded version of Seria collector.

4. parallel Scavenge COLLECTOR: a replication algorithm collector that controls the throughput for garbage collection. The throughput is the ratio of the time when the CPU runs the user code to the total CPU running time, you can control the efficiency of garbage collection by setting the throughput.

5. Parallel Old COLLECTOR: the Old version of Parallel Scavenge, which adopts the marking algorithm and multithreading for collection.

6. cms collector: to obtain the shortest recovery pause time as the target collector, the tag-clearing algorithm is used. The recycling process is: initial mark-> concurrent mark-> re-mark-> concurrent clear.

7. G1 COLLECTOR: The latest collector. The collection process is: initial mark-> concurrent mark-> final mark-> filter recycle. 

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.