Garbage collection and memory allocation algorithms

Source: Internet
Author: User

3.1 Judging if the object is dead reference-counting algorithm

Add a reference counter to the object, and whenever there is a place to reference it, the counter value is incremented by 1, and when the reference fails, the counter value is reduced by 1, and the object with the counter 0 at any time is impossible to use again.

3.2 Judging if the object is dead reference-accessibility analysis algorithm

In the mainstream programming language, the accessibility analysis is used to determine whether an object survives. The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, the path of the search is called the reference chain, when an object to the GC Roots no reference chain connected, it proves that this object is not available.

In the Java language, the objects that can be used as GC roots include the following:

Objects referenced in the virtual machine stack

Object referenced by class static property in method area

Objects referenced by constants in the method area

Objects in the local method stack that are referenced by JNI

3.3 Mark-Clear algorithm

The most basic collection algorithm is the "Mark clear" algorithm, like its name, the algorithm is divided into "mark" and "clear" two stages: first mark out all the objects that need to be recycled, after the mark is complete, the unified collection of all tagged objects.

3.4 Copy algorithm

To solve the efficiency problem, a collection algorithm called "Replication" appears, which divides the available memory into chunks of equal size by capacity, using only one piece at a time. When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once.

3.5 Labeling-Sorting algorithm

According to the characteristics of the old age, someone proposed another "marker-collation" algorithm, the marking process is still the same as the "tag-purge" algorithm, but the subsequent step is not directly to the recyclable object cleanup, but instead of all the surviving objects to move to one end, and then directly clean out the memory outside the end of the boundary.

4. Garbage collector

4.1Serial Collector

A new generation collector, this collector is a single-threaded collector that, when it is garbage collected, must suspend all other worker threads until it is collected at the end.

4.2ParNew Collector

The Parnew collector is actually a multithreaded version of the serial collector, and the rest behaves the same, in addition to using multiple threads for garbage.

The Parnew Collector is also the default Cenozoic collector after you use the-XX:+USECONCMARKSWEEPGC option, or you can use the-XX:+USEPARNEWGC option to force it to be specified. In the case of very many CPUs, you can use the-xx:parallelgcthreads parameter to limit the number of threads that are garbage collected.

4.3Parallel scavenage Collector

The Parallel scavenage Collector is a new generation collector that also uses a collection of replication algorithms and is a parallel multi-threaded collector.

The goal of the Parallel Scavenage collector is to achieve a controllable throughput. throughput = Run user code time/(run user code time + garbage Collection Time)

The Parallel scavenage Collector provides two parameters for precise control of throughput, controlling-xx:maxgcpausemillis parameters for maximum garbage collection pause times, and-xx:gctimeratio parameters that directly set throughput size.

Parallel Scavenage Collector also has a parameter-xx:useadaptivesizepolicy, when this parameter is opened, you do not need to manually specify the size of the Cenozoic, Eden and the ratio of survivor and other parameters.

4.4Serial Old Collector

Serial old is an older version of the Serial collector, which is also a single-threaded collector, using the "mark-and-organize" algorithm.

4.5ParallelOld Collector

Parallel old is an older version of the Parallel scavenge collector, using multithreading and the "mark-and-organize" algorithm.

4.6CMS Collector

The CMS collector is a collector with the goal of obtaining the shortest recovery pause time.

The CMS collector is implemented based on the "tag-purge" algorithm, which is more complex than the previous collectors, and the process is divided into 4 steps:

1, initial tag, 2, concurrent tag, 3, re-tag, 4, concurrent purge

Where the initial tag, re-tagging these two steps still require "Stop theworld".

There are 3 disadvantages of the CMS collector:

1, very sensitive to CPU resources, 2, unable to handle floating garbage, 3, there will be a large number of space debris generation.

4.7G1 Collector

G1 has the following features: 1, parallel and concurrency, 2, generational collection, 3, space integration, 4, predictable pauses.

With the G1 collector, the memory layout of the Java heap differs greatly from that of other collectors, which divide the entire Java heap into separate, equal-sized areas, while preserving the concepts of the Cenozoic and old eras, but the new and old generations are no longer physically isolated, and they are all part of the region's collection.

The action of the G1 collector is broadly divided into the following steps:

1, initial mark, 2, concurrent tag, 3, Final tag, 4, Filter and recycle

Garbage collection and memory allocation algorithms

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.