Java garbage Collection (2) __java

Source: Internet
Author: User
Tags garbage collection

The following is the author of the previous translation of garbage collection related articles Hotspot GC overview

Parallel Scavenge (parallel recycle)

In this article, we will describe how parallel garbage collection (Parallel GC) works. Specifically, this is a combination of running the Parallel scavenge collector in the Eden space, running parallel markers (Parallel mark) and sweep collectors in the old age (tenured generation) space. You can get these features by passing in the parameter '-XX:+USEPARALLELOLDGC ', although it is default on some machine types.

If you don't have a general understanding of the contents of the garbage collector, you can read the first article on garbage collection first.

Eden and survivor space

In the parallel recycling collector, Eden and survivor space are recycled using the well-known hemispheric GC method. Object initial allocations are stored in the Eden space, and once Eden is out of space, the garbage collection (GC) of the Eden space will be triggered. When garbage collection identifies the accessible objects and replicates these accessible objects to the active survivor space. The entire Eden space is then treated as an idle, contiguous block of memory that can be reallocated.

In this case the allocation process is like slicing a piece of cheese. Each piece will be cut in a row, and the slices next to it will be "eaten", and the advantage is that the allocation only requires pointers to be added.



In order to identify the object to be reached, the object graph should be retrieved. Retrieves the start of a set of "root" objects that are guaranteed to reach, for example, each thread has a root object. The search then finds the object that the root set points to, and then expands outwards until all the available objects are found. The following figure shows the process well.



Parallelism (parallel) in the context of parallel recycling (parallel scavenge) refers to running multiple threads at the same time to complete garbage collection. Never be confused with an incremental GC (incremental GC), where the collector can run concurrently or across the application. Parallel recycling improves overall GC throughput by better use of modern multi-core CPUs. Parallel attributes are implemented by giving each thread a set of root tags and part of the object table.

There are two survivor spaces (survivor spaces), but only one of them is active at all times. The way they recycle garbage is consistent with the way the Eden space is recycled. The main idea is that when objects are promoted from the Eden space (objects that survive in the Eden Space for garbage collection) These objects will be copied into the active survivor space (survivor spaces). Then, when the space is withdrawn, they are copied to the inactive survivor space (survivor spaces). Once active survivor space (survivor spaces) is completed, inactive survivor space (survivor spaces) is immediately activated and deactivated active space becomes inactive. This is done primarily by pointing the pointer at the beginning of the survivor Space (survivor spaces), and means that all the unreachable objects in the survivor space (survivor spaces) can be freed at the expense of assigning a single pointer.

New generation design versus time tradeoff (young Gen designs and times tradeoffs)

Because it involves only copy-accessible objects and real-time transform pointers, the amount of recovery and accessibility of the Eden Space and survivor space (survivor spaces) is directly proportional to the number of objects. This is important because, because of the generational hypothesis (generational hypothesis), we know that most objects are unreachable at an early age and that there is no GC cost to release the associated memory.

The design inspiration for the survivor (survivor spaces) space is inspired by the idea that earlier recycling is less expensive than recycling in old age space (tenured spaces). The CG runtime object continues to be recycled in a hemispheric way to improve the overall throughput of the system.

Eventually, the Eden Space is organized into a single contiguous space, making the cost of allocating objects very low. The C program may use the "malloc" command to allocate memory blocks, which will involve traversing some of the column's free memory space to try to find a large enough chunk of memory. When you use arena allocator and assign the contiguous space you need, all you have to do is check if there is enough free space and move the pointer in increments of the object size.

parallel tagging and scavenging (Parallel mark and Sweep)

After a certain number of recycling, the surviving objects into the old generation space (tenured spaces). The number of times they survived is called "tenuring threshold (old age threshold, also known as elevation threshold)", and the recycling work of the old age space is different from that of the Eden Space, which uses an algorithm called tagging and clearing (mark and sweep). Each object has a bit of markup associated with it. These tag initializers are set to false, and when the object graph is retrieved, the object's markup is set to true if it reaches.

The graph retrieval of the recognizable object is similar to the retrieval description of the Cenozoic (young Generation). The difference is that you use tokens to replace the copy of the object you are reaching. After the tag is complete, you can traverse the object table and free up space occupied by the unreachable object. This process is done in parallel through multithreading, and each thread retrieves one area of the heap.

Unfortunately, the process of deleting unreachable objects makes the old age space look like Swiss cheese (Swiss Cheese). tenured You get some of the used memory that can store objects, and there is a gap between the memory space and the memory space used by some of the past accessible objects. This memory fragmentation is very detrimental to the execution efficiency of an application because it makes it impossible to allocate larger objects in these empty memory spaces.


Cheese after Mark and Sweep.

To reduce problems like Swiss cheese (Swiss Cheese), parallel tag/purge (Parallel mark/sweep) compresses the heap to assign contiguous objects to the beginning of the old age space (tenured spaces). After the object is deleted, the area of the old age space (tenured spaces) is retrieved to determine those areas with low occupancy and those with high regional occupancy. The available objects in the area with low occupancy will be moved to a high occupancy area. These operations are naturally at the bottom of the memory at the previous compression stage, where the object's movement is actually performed by the thread assigned to the target area, rather than the source region (region).


Low occupancy cheese.

Summary

Parallel recycling divides the heap into four spaces: Eden, two survivor space (survivor spaces), and older generation space (tenured).

Parallel recycling uses parallel, copy collectors to reclaim Eden and survivor space (survivor spaces).

Different algorithms are used in the old age space (tenured spaces). The algorithm marks all objects, deletes the unreachable objects, and compresses the space.

Parallel recycling has good throughput, but the entire application is paused when it runs.

Original: https://www.javacodegeeks.com/2013/06/garbage-collection-in-java-2.html

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.