Java Profiling]sun JVM memory management and garbage collection

Source: Internet
Author: User

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. This article describes the memory management and garbage collection of Sun JVM 6.0.

1. Memory Management
In the process of running the program, will create a large number of objects, these objects, mostly short-period objects, a small part of the long-period objects, for short-cycle objects, the need for frequent garbage collection to ensure that useless objects are released early, for long-period objects, you do not need frequency garbage collection 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 mainly store class definitions, bytecode and constants, etc. rarely changed information, about this piece of garbage collection strategy can refer to my other blog "Tomcat Context reloadabled and OutOfMemory ( Permspace) ".
Class data Sharing (CDS) (http://java.sun.com/j2se/1.5.0/docs/guide/vm/class-data-sharing.html) is a new feature introduced by JDK5, The use of sharing some class definition information (Bootstrapclassloader loaded classes) between virtual machines accelerates the startup and memory usage of the JVM, primarily for clients, and it is best to close the CDs if the class needs to be instrutment. (By default, the JVM's server mode turns off cds,client mode will open CDs)

-xshare:off
Disable class data sharing.
-xshare:on
Require class data sharing to is enabled. If It could not is enabled for various reasons, the print an error message and exit.
-xshare:auto
The default; Enable class data sharing whenever possible.

We look at the display of the above areas through Jconsole (), from left to right respectively Edenspace, a suvivor Space, tenured gen, Code Cache, Perm Gen (shared-wr), Perm Gen ( Shared-ro), Perm Gen

2. Garbage Collection Policy
Two important metrics for evaluating a garbage collection strategy are:

    • Throughput (throughput): The longer the JVM spends on garbage collection, the lower the throughput
    • Pause time: The JVM garbage collection process has a pause period during which the application cannot run and the pause time is the length of the pause period

Unfortunately, these two indicators are generally conflicting, improving one of them will affect the other, depending on the situation we decide whether to prioritize throughput or pause time, for applications that require real-time response, we need to prioritize the pause time, we need to prioritize throughput for running applications in the background.
Before examining the various garbage collector, we need to understand a few important strategies

    • Parallel (Parallel): Parallel means the use of multiple threads for garbage collection at the same time, which generally improves pause times and throughput at the same time, and on servers with multiple CPU cores, this is basically the policy we want to use.
    • Concurrency (Concurrent): Some of the work of a parallel representation of the garbage collector, such as spam tags, is performed concurrently with the application, which further shortens the pause time, and it is important to note that the garbage collector's complexity increases substantially, essentially reducing throughput.
    • Memory fragmentation: There are three strategies that do not compress, compress, and copy, in space, the copy will cost more memory (for example, young Gen with memory management, need to maintain an additional suvivor space), in the time, the non-compression will reduce the memory allocation efficiency when the object is created, on garbage collection, Copy policies are more efficient than compression policies.

The Sun JVM has 4 garbage collector:

      • Serial Collector: Serial garbage collector, the garbage collector uses a single-threaded garbage collection for young Gen and tenured Gen, which uses a copy policy to avoid memory fragmentation for old Gen, which uses a compression strategy to avoid memory fragmentation. Basically, this approach should be avoided on servers on the kernel. Use-XX:+USESERIALGC to enable serial Collector in the JVM startup parameters.
      • Parallel Collector: Concurrent garbage collector, the garbage collector uses multi-threaded parallel garbage collection for young Gen and tenured Gen, and for Young Gen, the copy policy is used to avoid memory fragmentation, to old Gen, The compression policy is used to avoid memory fragmentation. Use-XX:+USEPARALLELGC to enable parallel Collector in the JVM startup parameters.
      • Parallel compacting Collector: A parallel compression garbage collector, similar to Parallel Collector garbage collection, but uses a more efficient garbage collection strategy for tenured Gen, which can be shorter on pause times. Use-XX:+USEPARALLELOLDGC to enable parallel compacting Collector in the JVM startup parameters.
      • Concurrent mark-sweep (CMS) Collector: The concurrency flag clears the garbage collector, for young Gen uses the same garbage collection strategy as parallel Collector, for tenured Gen, Garbage-collected garbage-flagged threads are at the same time as the application thread, while garbage removal requires suspending the application thread, but the pause time is greatly reduced, and it is important to note that the overall throughput is reduced because of the complexity of the garbage collection process.
      • Reprint: http://ayufox.iteye.com/blog/652205

Java Profiling]sun JVM memory management and garbage collection

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.