JVM garbage collection mechanism (i)

Source: Internet
Author: User

  Garbage collector

  The garbage collector is primarily responsible for:

    1. allocating memory;

2. Ensure that all objects being referenced still exist in memory;

3. The collection is no longer the memory of the object referenced by the program;

The garbage collector provided by the Hotspot VM is a generational garbage collector (generational GC) that divides memory into different stages, where objects of different lifecycles are placed in different memory spaces. This design is based on the weak-age hypothesis (Weak generational hypothesis): Most objects quickly become unreachable, and only a few old objects refer to the new object . This way, you can reduce the downtime of garbage collection and the cost of recycling large-scale objects. The Hotspot VM divides its heap space into 3 generational spaces:

1. younger generation (young Generation)

A. Java applications when allocating Java objects, these objects are assigned to the young generation heap space;

B. This space is mostly small objects and will be recycled frequently;

C. Because the garbage collection of the young generation heap space is very frequent, the garbage collection algorithm will pay more attention to the recovery efficiency;

2. old generationn

A. The long-term survival of the young generation of heap space will be transferred to (or perhaps permanently transferred) the old generation of heap space;

B. This heap space is usually larger than that of the younger generation, and its space is growing slowly;

C. Since most of the JVM heap space is allocated to older generations, the garbage collection algorithm needs to be more space-saving, and the algorithm needs to be able to handle heap space with low garbage density;

3. Persistent Generation (Permanent Generation)

A. Storing metadata for VMS and Java classes (metadata), as well as static variables for interned strings and classes;

The heap space is as follows:

Its young generation is divided into three spaces:

1. eden--Most of the objects that have just been created will be stored here;

2. After the first GC was performed in Eden Space, the surviving objects were moved to survivor1--.

3. Survivor2;

Note: When a survivor space is saturated, the surviving objects are moved to another survivor space. It then empties the survivor space that is already saturated, and the survivor space must be at least one empty. If two survivor spaces have data, or two spaces are empty, that must indicate some sort of error in your system.

  GC Type

JDK7 offers a total of 5 GC types:

1. Serial GC;

2. Parallel GC;

3. Parallel Old GC (Parallel compacting GC);

4. Concurrent Mark & Sweep GC (or "CMS");

5. Garbage first (G1) GC;

Where the Serial GC should not be used on the server. This GC type exists in the desktop era of a single-core CPU. Using the serial GC can significantly reduce your application's performance metrics.

  

  Garbage Collection method

The garbage collection mechanism works when the three-generational heap space is tense or there is not enough space to allocate new requests. There are two kinds of garbage collection methods: The Secondary collection (Minor GC) and the full collection (fully GC). When the young generation heap is full, it triggers the minor GC to move the surviving objects to the old generation heap space. When the old generation heap is full, it triggers a full GC that covers the entire range of object heaps.

Secondary collection

1. When the young generation heap space is tense, it will be triggered;

2. The collection interval is short compared to full collection;

Full collection

1. When the old age or the permanent heap space is full, it will trigger the full collection operation;

2. You can use the System.GC () method to explicitly start the full collection;

3. Full collection generally depends on the size of the heap, the time required varies, but generally longer;

Full collection typically takes a long time and is the main cause of the program's inability to delay execution or reach the throughput target. The goal of GC is to reduce the frequency of garbage collection during program operation. In order to achieve this goal, we can start with these two aspects:

1. Consider the system:

A. Try to use a large heap, but not as large as you need the system to "swap" pages from disk. In general, 80% of the available RAM (which is not consumed by the system process) should be allocated to the JVM.

B. The larger the Java heap space, the better the garbage collector and Java applications are performing in terms of throughput (throughput) and deferred execution (latency).

2. Consider the application:

A. Reducing the objectallocationsoperation, or using objectretentionto help reduce the size of the surviving data, can in turn help garbage collection do better.

  What happens if the object of the old age needs to refer to a new generation of objects?

To solve this problem, there is a "card table" in the old generation, which is a byte -size block. References to young generations of objects of all older generations are recorded in this table. When performing GC for the younger generation, it is only necessary to query the card table to determine whether it can be collected without querying the entire old age. This card table is managed by a write barrier . Write barrier has brought a significant performance boost to the GC, although there may be some overhead, but the overall time of the GC has been significantly reduced.

  

JVM garbage collection mechanism (i)

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.