Jvm gc Mechanism

Source: Internet
Author: User
Tags xms

[Java Performance Analysis] Sun JVM memory management and garbage collection
Memory Management and garbage collection are critical aspects of JVM. For Java performance analysis, it is very important to understand the basic memory management and garbage collection policies. This article describes Sun JVM 6.0 memory management and garbage collection.

 


1. Memory Management

During the program running, a large number of objects are created. Most of these objects are short-lived objects and a small part are long-lived objects. For short-lived objects, garbage collection needs to be performed frequently to ensure that useless objects are released as soon as possible. For long-period objects, garbage collection does not need to be performed frequently to ensure unnecessary garbage scanning and detection. To solve this problem, Sun JVM's memory management adopts a generational strategy.

 

1) Young Gen: the Young generation mainly stores newly created objects. The memory size is relatively small and garbage collection is frequent. The young generation is divided into one Eden Space and two Suvivor spaces (named A and B)
When an object is created in the heap, it enters the Eden Space of the young generation.
During garbage collection, the garbage collector scans Eden Space and A Suvivor Space. If the object is still alive, copy it to B Suvivor Space. If B Suvivor Space is full, copy Old Gen
When scanning A Suvivor Space, if the object remains alive after several scans, the JVM considers it an Old object, and moves it to the Old Gen.
After scanning, the JVM clears the Eden Space and A Suvivor Space, and then exchanges the roles A and B (that is, the Eden Space and BSuvivor Space will be scanned during the next garbage collection.

We can see that the Young Gen garbage collection method copies the surviving objects to the empty Suvivor Space to ensure that there are no memory fragments, and the Space-for-time method is used to accelerate the memory garbage collection.

 

2) Tenured Gen: the old generation mainly stores objects that the JVM considers to be old (which still exists after several Young Gen garbage collection times ), the memory size is relatively large, and garbage collection is relatively less frequent (for example, it may take several hours ). The old generation mainly uses compression to avoid Memory fragments (moving surviving objects to the side of the memory chip). Of course, some garbage collectors (such as CMS garbage collectors) are efficient, it may not be compressed.

 

3) Perm Gen: the persistent generation mainly stores class definitions, bytecode, constants, and other information that is rarely changed.

 

 

1. Heap configuration and garbage collection
Java Heap is divided into three zones: Young, Old, and Permanent. Young stores the just-instantiated object. When the area is filled up, GC moves the object to the Old area. The Permanent area stores the reflection object. This section does not discuss this area.

 

You can use the-X parameter to set Heap allocation for JVM.-Xms corresponds to the initial Heap size,-Xmx corresponds to the maximum Heap size, and-Xmn corresponds to the Heap value of Yong generation.
(-Xms

Initial Heap size
-Xmx

Java heap maximum
-Xmn

Heap size of young generation)

JVM has two GC threads. The first thread is responsible for revoking Heap's Young zone. When the Heap is insufficient, the second thread traverses the Heap and upgrades the Young area to the Older area. The size of the Older area is equal to-Xmx minus-Xmn, and the value of-Xms cannot be set too large, because the second thread is forced to run will reduce JVM performance.

Why does GC occur frequently in some programs? There are the following reasons:

1. The program calls System. gc () or Runtime. gc ().

2. Some middleware software calls its own GC method. In this case, you need to set parameters to disable these GC methods.

3. Java Heap is too small. Generally, the default Heap value is very small.

4. Frequently instantiate objects and Release objects. In this case, try to save and reuse objects, for example, using StringBuffer () and String ().
If you find that after each GC, the remaining Heap space will be 50% of the total space, which indicates that your Heap is healthy. Many Java programs on the Server end have a better free space of 65% after each GC.

 

Experience:
1. It is best to set-Xms and-Xmx to the same value on the Server JVM. To optimize GC, it is recommended that the-Xmn value be equal to 1/3 [2] of-Xmx.
2. It is best for a GUI program to run GC every 10 to 20 seconds and complete GC every time within half a second [2].

Note:
1. Increasing the Heap size reduces the GC frequency, but increases the GC time. When GC is run, all user threads are paused, that is, Java applications do not work during GC.
2. The Heap size does not determine the memory usage of the process. The memory usage of a process must be greater than the value defined by-Xmx, Because Java allocates memory for other tasks, such as the Stack of each thread.

2. Stack settings
Each thread has its own Stack.
-Xss

Stack size of each thread
The Stack size limits the number of threads. If the Stack is too large, the memory may leak. -The Xss parameter determines the Stack size, for example,-Xss1024K. If the Stack is too small, the Stack may leak.
3. hardware environment
The hardware environment also affects GC efficiency, such as the machine type, memory, swap space, and number of CPUs.
If your program needs to frequently create many transient objects, this will lead to frequent jvm gc. In this case, you can increase the machine memory to reduce the use of Swap space [2].
4.4 GC Types
The first type is single-thread GC, which is also the default GC ., This GC is applicable to single-CPU machines.
The second type is Throughput GC, which is a multi-thread GC. It is suitable for programs with multiple CPUs and a large number of threads. The second type of GC is similar to the first type of GC. The difference is that the GC is multi-threaded in the Young area, but in the Old area and the first one, the single thread is still used. -XX: + UseParallelGC parameter to start the GC.
The third type is Concurrent Low Pause GC, which is similar to the first type. It is suitable for multiple CPUs and requires shorter time for program stagnation caused by GC. This GC can be used to run applications in the Old zone. -XX: + UseConcMarkSweepGC parameter to start the GC.
The fourth type is Incremental Low Pause GC, which is applicable to the requirement to shorten the time of program stagnation caused by GC. This GC can be used to reclaim some Old objects in the Young area. -The Xincgc parameter starts the GC.
For details about the four types of GC, refer to [3].

References:
1. JVM Tuning. http://www.caucho.com/resin-3.0/performance/jvm-tuning.xtp#garbage-collection
2. Performance tuning Java: Tuning steps

Http://h21007.www2.hp.com/dspp/tech/tech_TechDocumentDetailPage_IDX/1,1701,1604,00.html

3. Tuning Garbage Collection with the 1.4.2 JavaTM Virtual Machine.

Http://java.sun.com/docs/hotspot/gc1.4.2/

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.