2. Garbage Collection and algorithm

Source: Internet
Author: User
Tags garbage collection

1, to solve what memory needs to be recycled;
2, when recycling;
3, how to recover;
If an object is Gc.root unreachable, first determine if his finalize method has not been rewritten, without rewriting the direct recycle, if overridden, then placed in a queue called F-queue, by a thread to execute the object's Finalize method,
After the Finalize method is executed, the recovery will again determine whether the gc,root is unreachable; This is an opportunity for the object to save himself;

Garbage collection algorithm for JVM
1. Tag-Clear algorithm
This is the most basic collection algorithm, (Mark-sweep), as the name expresses, scans the entire memory area, marks recyclable objects, and clears all tagged objects when the scan is complete.
The disadvantage of this algorithm has the following two points
Efficiency is not high.
Memory fragmentation is generated, so-called memory fragmentation is a discontinuous area of memory, which may result in enough memory but not enough contiguous memory to be allocated to a larger object, resulting in a FULL-GC

2. Copying algorithms
The idea of the replication algorithm is mainly to divide the memory area into two parts, when allocating memory only use part of the memory, that is, any time is guaranteed that part is not used, each time the collection of the surviving object is copied to another area, and then after the end of the collection to empty the current scanned area directly.

3. Labeling-Sorting algorithm
This algorithm is an improvement to the mark-sweep algorithm, that is, after scavenging the recyclable objects, the surviving objects are moved to one end, so that no memory fragmentation is caused.

4. Generation of collection algorithms
The generational collection algorithm divides the memory into several blocks according to the different life cycles of the objects, and uses different algorithms for these different regions.
Most of the virtual machines implemented are the Java heap is divided into the new generation and the old age, in general, the new generation of the use of replication algorithm, the old age is generally used to mark-clear or marker-collation algorithm

The new generation of memory models

Garbage collector:

A connection representative can use


1, single-thread + copy algorithm =serial collector;

2, single-threaded + mark Finishing algorithm =serialold collector;

3, multithreading + replication algorithm =parnew collector/Parallel scavenge collector;

4. Multi-thread + mark finishing algorithm =Parallel old collector;

Garbage collector is the implementation of garbage collection algorithm, according to the idea of generational collection, garbage collector can be divided into the new generation collector and Laosheng with collector, according to the number of recycled threads, can be divided into single-threaded collectors and multi-threaded collectors.
1.Serial Collector: Is the most basic, oldest collector, it is aSingle thread collector, and when workingmust stopAll user threads, which are used for the new generation of collectors, so adopt a copy algorithm, which isdefault new generation in client modeCollector.
2.ParNew Collector: actually isserial Multi-threaded version of the collectorIsdefault New generation collector in Server mode, only it and the serial collector can matchCMSCollectors work together.
3.Parallel Scavenge collector: Also a new generation collector, multi-threaded recycling, looks like andThere's no difference between parnew collectors ., which is characterized by the difference between the focus and other collectors, the Parnew collector focuses oneveryGC takes as much time as possible, while the parallel scavenge collector targets a controllable throughput, so-called throughput is the ratio of the CPU's time to running user code to the total CPU elapsed time,throughput = Total time to run user code/(total time to run user code + garbage collection Time), high throughput can be the most efficient use of CPU time, as soon as possible to complete the operation of the program tasks, mainly for the background operation andtasks that do not require too much interaction with the user, you can set the throughput through-xx:maxgcpausemillis and-xx:gctimeratio.
4.serial Old collector: YesSerial collection of old age versions, is also a single-threaded collector, using the tag-collation algorithm.
5.Parallel Old collector: Is the old version of the parallel scavenge collector, the multi-threaded collector, using the tagging-sorting algorithm, andmaximum throughput optimization with Parallel scavenge
6.cms Collector-A garbage collector for the service side, a collector based on the tag-purge algorithm, is a collector that really implements concurrent collection, which does not stop the user thread at GC time, and the entire process is divided into 4 steps: 1, initial tag 2, concurrency tag 3, re-tagging 4, concurrent cleanup, where initial markup and re-tagging still need to stop the user thread, But the pause time is not very long, it is an excellent collector, concurrent collection of low pauses, but there are some drawbacks: 1, the CMS collector is more sensitive to CPU resources, because it is concurrent, it does not cause the user thread to pause, but it slows down the application because it consumes CPU resources .Total throughput decreased. 2.Unable to process floating garbage, floating garbage is the garbage generated by the user thread during concurrent cleanup, the CMS collector cannot clean them up in this collection, and because the user thread needs to run during the Collect phase, a portion of the space needs to be reserved for use by the user thread. 3, because the use of the tag-cleanup algorithm, will produce memory fragmentation.

7.G1 Collector:-A garbage collector for the service side, the advanced version of the CMS, can handle floating garbage, the memory area is divided into several regions, each decision is analyzed in the region to meet the limited conditions, the choice of recovering regions;

2. Garbage Collection and algorithm

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.