The GC mechanism of JVM and the tuning method of JVM

Source: Internet
Author: User
Tags xms

Memory management and garbage collection are critical points for the JVM, and it is important to understand the basic strategies of memory management and garbage collection for the analysis of Java performance.

1. During the process of running the program, a large number of objects are created, most of which are short-period objects, a small part is a long-period object, and for short-period objects, frequent garbage collection is required to ensure that useless objects are released early, for long-period objects, Frequency garbage collection is not required to ensure unnecessary garbage scanning detection. To address this contradiction, the Sun JVM's memory management uses a generational strategy.

1) Young generation (Young Gen): the younger generation mainly stores newly created objects, the memory size is relatively small, garbage collection will be more frequent. The young generation is divided into 1 Eden spaces and 2 Suvivor space (named A and B)
When the object is created on the heap, it enters the young generation of Eden Space.
When the garbage collector is garbage collected, it scans Eden Space and a suvivor space, and if the object is still alive, copy to B suvivor space, and if B suvivor space is full, copy the old Gen
When scanning a suvivor space, if the object has been scanned several times and still survives, the JVM considers it an old object and moves it to old Gen.
After the scan is complete, the JVM empties Eden Space and a suvivor space and swaps the roles of A and B (that is, the Eden Space and Bsuvivor space are scanned for the next garbage collection.)

We can see that young Gen garbage collection uses a way to copy the surviving objects to the empty Suvivor space to ensure that there is no memory fragmentation and to use space-time to speed up memory garbage collection.
2) Old generation (tenured Gen): Older generations mainly store objects that the JVM considers to be older (after several years of the garbage collection of young Gen), the memory size is relatively large, and garbage collection is relatively less frequent (for example, it may be a few hours). Older generations are mostly compressed to avoid memory fragmentation (moving the surviving objects to one side of the memory slice), and of course, some garbage collector (such as the CMS garbage collector) may not compress for efficiency reasons.
3) Persistent generation (Perm Gen): Persistent generations primarily store information that rarely changes, such as class definitions, bytecode, and constants

2. Summary

1, the object priority in Eden allocation, here Most of the objects have the characteristics of the Minor, the GC mainly clean up the place
2. Large objects (large memory), old objects (frequently used)

3, survivor can not accommodate the object, will enter the old age, full GC mainly clean up the place

3.JVM has 2 GC threads

The first thread is responsible for recovering the young area of the heap
The second thread traverses the heap when the heap is insufficient, and upgrades the young area to the older area

The JVM, which uses a generational recycling strategy, YGC the young generation at a higher frequency, while the old object (tenured generation) is less (tenured generation full) to perform fully GC. This makes it unnecessary to check all the objects in memory every time the GC is over.

The GC does not clean up permgen space during the main program's run time, so if you have many classes in your application (especially the dynamically generated class, and of course PermGen space is not limited to classes), PermGen space errors are likely to occur.

4.JVM Tuning

Reduce the number of FULLGC, think FULLGC will suspend the program for a long time, if the number of FULLGC more. The program will be suspended from death.

Heap Size Setting

The JVM heap setting is the set of memory space that the JVM can provision during the run of the Java program. The JVM automatically sets the value of the heap size when it starts, and its initial space (that is,-XMS) is 1/64 of the physical memory, and the maximum space (-XMX) is 1/4 of the physical memory. You can use options such as the-XMN-XMS-XMX provided by the JVM to set it up.

If the heap size setting is small, the GC takes up more time and the application is assigned less execution time. However, the heap size does not exceed 80% of the available physical memory.

with 2G or more of physical memory

Http://www.xuebuyuan.com/1201516.html

The GC mechanism of JVM and the tuning method of JVM

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.