Java garbage collection algorithm and garbage collector

Source: Internet
Author: User

garbage collection algorithms, garbage recovery algorithms, Java garbage collector

Directory
1. Garbage collection algorithm
1) Reference counting method
2) Root Search method
2. Garbage collection algorithm
1) Copy algorithm
2) mark-Clear algorithm
3) Labeling-sorting algorithm
4) Generational collection algorithm
3. Java Garbage collector
New Generation GC:
1) Serial garbage collector
2) parnew garbage collector
3) Parallel Scavenge garbage collector
Old age GC
1) Serial and parallel scavenge have a corresponding version of the old age
2) CMS garbage collector
Memory allocation location for 4.java objects
5.java Tlab

1. garbage Collection algorithm

1 ) Reference Counting Method

To add a reference counter to an object, when the Reference object is counter +1, when the reference fails, counter-1, when the counter equals 0 o'clock, the object is invalidated, the memory can be recycled.

Advantages: simple and efficient.

Cons: Cross-referencing between objects is not a good solution.

2 ) Root Search method

An object path that can be reached through GC roots is called a reference chain (reference chain), and when an object has no reference chain (that is, it is unreachable from GC roots), memory can be reclaimed. Java uses this algorithm for garbage collection.

What objects can be considered GC roots?

A. Reference object in the virtual machine stack (that is, local variables in the stack frame);

B. reference objects in the local method stack;

c. object referenced by static variable in method area (permanent generation) and constant pool reference

2. garbage Collection algorithm

1 ) Replication Algorithms

Divide the memory into two parts (equal in size), use only one chunk at a time to allocate memory, and when memory is exhausted, go to GC, copy the surviving object directly to another free memory, then clear all the memory blocks that are currently in use and then go to another piece of memory for use.

Advantages: simple and efficient.

Cons: Waste memory because there is another piece of memory idle every time.

2 ) Mark - Purge Algorithm

In two steps, the first step is to mark the objects that can be reclaimed, and the second step is to clean up the object memory that can be reclaimed.

Cons: The first mark and purge steps are inefficient, followed by memory fragmentation.

3 ) Mark - Collation Algorithm

Similar to the tag-purge algorithm, but in the second step of memory reclamation, the surviving object is moved to one end of the memory to eliminate the memory fragmentation problem.

4 ) Generational collection algorithm

Java Sun hotspot Virtual machine divides the memory into the Cenozoic (heap), the old Generation (heap), the permanent generation (the method area, the Chang, the real-time compiles the code) several regions, the new generation mainly uses the replication algorithm based garbage collection, the old age and the permanent generation mainly uses the mark-sorting algorithm to carry on the garbage collection. Which garbage collection algorithm is used in each region depends on the implementation of the Collector.

3. Java garbage collector

Cenozoic GC Device:

1 ) Serial garbage collector:

Single-threaded serial garbage collector, which uses a replication algorithm for garbage collection, requires the GC to pause all user threads until the GC is complete.

Note: The serial meaning in the serial garbage collector here means that the GC process and the user thread execution process are serial, that is, the user thread is paused during the GC process, and the user thread executes Yes, the GC is not executed.

2 ) parnew garbage collector:

Multi-threaded serial garbage collector, serial multi-threaded version, other features with serial.

3 ) Parallel Scavenge garbage collector:

Similar to Parnew, but the collector focuses on the throughput of the CPU (THROUGHPUT=USER-CPU-TIME/TOTAL-CPU), via Parameters-xx:gctimeration and-XX: Maxgcpausemillis to control throughput, is a throughput-first collector, and also uses a replication algorithm for garbage collection, which requires the GC process to suspend all user threads.

old age GC Device:

1 ) Serial and the Parallel Scavenge have a corresponding version of the old age:

Serial and parallel old garbage collectors, which differ from the new generation, are based on the version of the older region and are garbage collected using the tagging-sorting algorithm.

2 ) CMS garbage collector:

Concurrent Mark sweep Collector, is the real multi-threaded parallel garbage collector, CMS in the GC process in some stages of the user thread is able to run, so it is the real sense of the parallel garbage collector, the garbage collector described earlier in the GC process to suspend the user thread, Therefore, they are treated as a garbage collector that executes serially with the user thread. The CMS GC process is completed in four steps:

A. initial tag: Just mark the GC Roots objects that can be directly associated are fast and still need to suspend all worker threads.

B. Concurrency token: GC Roots the process of tracing, working with the user thread, does not require a worker thread to be paused.

c. re-tagging: In order to fix the tag record of the part of the object that caused the markup to change while the user program continues to run during concurrent tagging, all worker threads still need to be paused.

d. Concurrent Purge: Clear GC Roots unreachable objects, working with user threads, do not need to pause worker threads.

4. memory allocation location for Java objects

1) The compiler determines whether the object is allocated on the stack or on the heap through escape analysis. If it is allocated on the heap, enter option 2.
2) if tlab_top + size <= tlab_end, the object is allocated directly on Tlab and the value of tlab_top is increased, if the existing tlab is insufficient to hold the current object 3.
3) reapply for a tlab and try to store the current object again. If it does not fit, then 4.
4) in the Eden area locking (this area is multi-threaded shared), if eden_top + size <= eden_end The object is stored in the Eden area, the value of eden_top is increased, and if the Eden area is not sufficient to store, then 5.
Executes a young GC (minor collection).
5) After young GC, if the Eden Zone is not sufficient to hold the current object, it is allocated directly to the old age.

4. java Tlab
The JVM opens up a small piece of thread-private area in the memory Cenozoic Eden Space, called Tlab (thread-local allocation buffer). The default is set to occupy 1% of Eden space. In Java programs, many objects are small objects and lost, they do not exist thread sharing is also suitable for fast GC, so for small objects usually the JVM will be assigned priority on Tlab, and the allocation on Tlab because the thread is private, there is no lock overhead. Therefore, it is often more efficient to allocate multiple small objects in practice than to allocate a large object.
In other words, every thread in Java has its own buffer called Tlab (thread-local allocation buffer), and each tlab has only one thread to operate on. Tlab combined with Bump-the-pointer technology allows for fast object assignment without the need for any locks to be synchronized, that is, when the object is allocated without locking the entire heap, but only in its own buffer allocation.
The JDK source code for object assignment can be found in the Java object creation [initialization] of the JVM for the analysis of the OPENJDK source code.

Reference:

Garbage collection algorithm and garbage recovery algorithm:

Http://www.2cto.com/kf/201401/272681.html

Java Garbage Collector:

(recommended) http://blog.csdn.net/chjttony/article/details/7883748

Http://www.cnblogs.com/wrencai/articles/4232264.html

Memory model:

Http://www.cnblogs.com/wrencai/articles/4232264.html

Are Java objects allocated on the heap:

Http://blog.hesey.net/2011/07/object-allocation-on-non-heap.html

Java Escape analysis and Object memory allocation, Tlab:

http://blog.csdn.net/yangzl2008/article/details/43202969

Java garbage collection algorithm and garbage collector

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.