JVM's garbage collector (GC) and memory allocation policy

Source: Internet
Author: User

1. Why should I study GC?

The GC (Garbage Collection) appeared earlier than Java, and the first use of the GC in Lisp in the 60 's.

When you need to troubleshoot various memory overflows and memory funnel problems, GC is needed when garbage collection becomes a bottleneck for the system to reach a higher level of concurrency.

In short, writing a high-performance Java program needs to understand GC.

2.GC location in the JVM's architecture

HotSpot JVM Architecture.

The parts related to the application performance are marked in purple, and the tuning begins with them!

3. What is performance?

When tuning Java applications, the focus is on two points: response speed and throughput.

3.1 Response Speed

Response speed is the speed at which the application or system returns the specified data. Example:

    • The desktop UI corresponds to the speed.
    • The website returns the page speed.
    • Database query speed.
3.2 Throughput

Throughput focuses on the amount of work done over time. Example:

    • The number of transactions within a given time period.
    • Volume of jobs processed in an hour.
    • The amount of database queries in an hour.
4. Automatic garbage collection

In short, the process of automatic garbage collection is to see in the heap memory which objects are being used, which ones are not used, and delete objects that are not used.

The object being used has a reference to it somewhere in the program, and the object that is not being used is no longer referenced. In C, allocating and reclaiming memory is manual. Garbage collection in Java is done by GC.

4.1 Basic process of garbage collection

First step: Mark

Tags are referenced and objects that are no longer referenced.

This process can be time-consuming if all the objects in the system are scanned. Foreshadowing

Step two (one option): Normal purge.

Deletes all objects that are not referenced, preserves the referenced object, and points the pointer to free space.

The memory allocator holds a pointer to the free space.

Step Two (another option): Clear and tidy.

To improve performance, after removing the non-cited object, we put together all the remaining cited objects so that the free space can also be a contiguous chunk. This makes the new memory allocation more concise and faster.

4.2 Most of the objects are short-lived.

After a lot of analysis, most of the objects are short-lived. As shown, the y-axis represents the number of bytes of the surviving object, and the x-axis represents the number of bytes that the GC has allocated over time.

It can be seen that most objects survive for a short time.

The generation of 4.3JVM

The above-mentioned foreshadowing, scan all the objects in the SVM, it is very inefficient to mark the collation. As the number of objects increases, the GC's time-consuming will also increase.

In 4.2 We talked about the short-lived majority of the objects. So we can recycle the JVM to improve performance. Divide the heap into several parts: The new generation, the old age and the permanent generation.

4.3.1 Cenozoic

All new objects are assigned here. When the Cenozoic is full, it will depart minor garbage collection (referred to as minor GC), recovering a new generation full of "dead" objects will soon be. Some of the surviving objects are aged and put into the old age after reaching a certain threshold.

Stop the World event: all minor GC are stop the world event. This means that all threads will be stopped until the GC is complete.

The old age of 4.3.2

Old age to store long-lived objects. When the age of the new generation exceeds a certain threshold, it enters the old age. For the old age recycling is called major garbage collection (major GC).

Major GC is also the stop the World Event. Generally major GC is much slower than minor GC because it involves all surviving objects. Therefore, for applications with high response times, major GC should be avoided as much as possible.

In addition, the duration of the major GC depends on what recycling strategy the GC uses to recycle the old age.

4.3.3 Permanent Generation

Permanently stores metadata that describes classes and methods.

5. Is the object dead?

How can I tell which objects are "alive"? What has "died"? The general use of reference counting algorithm and accessibility analysis algorithm.

5.1 Reference Counting algorithm

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

This algorithm is simple to realize and has high efficiency. Python uses this algorithm to manage memory. But there is no reference counting algorithm in the mainstream Java virtual machine to manage memory. The main reason is that it is difficult to solve the problem of mutual circular references between objects.

/*** After the TESTGC () method is executed, will obja and OBJB be GC? * @authorZzm*/ Public classREFERENCECOUNTINGGC { PublicObject instance =NULL; Private Static Final int_1MB = 1024 * 1024; /*** The only meaning of this member property is to occupy a bit of memory so that it can be seen in the GC log if there is a recycle*/    Private byte[] Bigsize =New byte[2 *_1MB];  Public Static voidTESTGC () {REFERENCECOUNTINGGC Obja=NewREFERENCECOUNTINGGC (); REFERENCECOUNTINGGC OBJB=NewREFERENCECOUNTINGGC (); Obja.instance=OBJB; Objb.instance=Obja; Obja=NULL; OBJB=NULL; //Suppose Gc,obja and OBJB can be recycled in this line? System.GC (); }}
5.2 Accessibility Analysis algorithm

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 downward, the path that the search passes is called the reference chain. This object is not available when an object is connected to the GC roots without any reference chain (in the case of graph theory, the GC root is not available to this object). Such as.

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

    • The object referenced in the virtual machine stack (the local variable table in the stack frame).
    • The object referenced by the class static property in the method area.
    • The object referenced by the constant in the method area.
    • The object referenced by JNI (that is, generally speaking, the native method) in the local method stack.
6. GC Process

It explains why the heap is divided into different recycling generations, and how the GC process has been completed in collaboration between different recycling generations. The transfer of object assignment and the recycle generation is described in slices.

In the first step, all new objects are allocated in Eden Space, and two surviror spaces are just beginning to empty. Such as.

The second step, when the Eden space is full, triggers the minor GC. Such as.

In the third step, the referenced object is moved to the S0 space, and the unreferenced object is deleted when the Eden is cleared. Such as.

Fourth, at the next minor GC, the Eden operation is the same as the third step, that is, the unreferenced object is deleted, the cited object is moved to the survivor space, but it is moved to S1.

In addition, the age at which the last minor GC survived was increased and moved to S1 in S0 space. When all the surviving objects are moved to S1, S0 and Eden are cleared. Such as.

The fifth step, in the next minor GC, repeats the same process as the fourth step, except survivor space swaps. Moving the surviving objects to S0, the surviving objects grow older, and Eden and S1 are cleared. Such as.

The sixth step, the age of the object increases each time the GC minor, when the object ages above a certain threshold (assuming 8), then these objects move from the Cenozoic to the old age. Such as.

Seventh, as minor GC continues to occur, there are constant objects moving from the Cenozoic to the old age. Such as.

The eighth step, as seen from the above, is that most of the GC's processes are in the Cenozoic. Until the major GC is triggered, the old age is cleared and sorted out. Such as.

7. Garbage collector

7.1Serial Collector

Serial GC is generally not used on the server side. However, the virtual machine runs the default Cenozoic collector in client mode because it is simple and efficient (compared to the single thread of other collectors).

7.2ParNew Collector

7.3 Parallel Scavenge Collector

7.4 Serial Old Collector

7.5 Parallel Old Collector

7.6 CMS Collector

7.7 G1 Collector

8. Memory allocation and recycling policy 8.1 objects first in Eden assignment

1 Private Static Final int_1MB = 1024 * 1024;2 /**3 * VM parameters:-verbose:gc-xms20m-xmx20m-xmn10m-xx:+printgcdetails-xx:survivorratio=84   */5  Public Static voidtestallocation () {6      byte[] allocation1, Allocation2, Allocation3, Allocation4;7Allocation1 =New byte[2 *_1MB];8Allocation2 =New byte[2 *_1MB];9Allocation3 =New byte[2 *_1MB];TenAllocation4 =New byte[4 * _1MB];//one minor GC appears One}
8.2 Large objects go straight into the old age

1 Private Static Final int_1MB = 1024 * 1024;2 /**3 * VM parameters:-verbose:gc-xms20m-xmx20m-xmn10m-xx:+printgcdetails-xx:survivorratio=84 *-xx:pretenuresizethreshold=31457285  */6  Public Static voidTestpretenuresizethreshold () {7     byte[] allocation;8allocation =New byte[4 * _1MB];//direct distribution in the old age9}
8.3 Long-lived objects will enter the old age

8.4 Dynamic Object Age Determination

8.5 Space Allocation Guarantee

9. Summary

This article describes garbage collection, processes, garbage collection algorithms, garbage collectors, and various principles. GC is one of the main factors that affect system performance and concurrency in many cases. In short, want to write high-performance Java program, understand GC is necessary!

Resources:

Deep understanding of JVM Zhou Zhiming

Http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

http://www.cubrid.org/blog/dev-platform/understanding-java-garbage-collection/

JVM's garbage collector (GC) and memory allocation policy

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.