Java Virtual machine--new generation and old age GC

Source: Internet
Author: User
Tags xms

1. Distribution of generations in the Java heap:

Figure 1:java Distribution of each generation in a heap

Young: It is mainly used to store new objects.

Old: Primarily stores memory objects that have a long life cycle in the application.

Permanent: Refers to the memory of the permanent storage area, the main storage class and meta information, class in the load is loaded into the PermGen space area. Unlike the heap area where instance is stored, GC (garbage Collection) does not clean up permgen space during the main program run time, so if your app will load many classes, it will likely appear permgen Space error.

2. What is the GC algorithm used by the JVM?

Collection of generations.

The memory is divided into several regions, and the objects of different life cycles are placed in different regions;

Frequent collection of areas with short life cycles when GC is collected (young area);

Areas with a relatively small collection life cycle (old area);

The base does not collect the permanent zone (Perm area).

3. What is the difference between GC and full GC?

GC (or minor GC): Collects areas with short life cycles.

Full GC (or major GC): Collects the entire heap in areas with short life cycles (young area) and a long life cycle (old area).

Their collection algorithms are different, so the time used is different. GC efficiency is also high, we want to minimize the number of full GC. When the display calls System.GC (), GC does a full collection (both young generation and tenured generation).

4. Is Eden empty after Minor GC?

Yes, the Minor GC will move all live objects in Eden to the Survivor area, and if the survivor area doesn't fit, the remaining live objects will be moved to old generation.

5. Garbage collection Options (JDK1.4):

Figure 2:GC Parameters

Heap Settings
-XMS: initial Heap Size
-XMX: Maximum Heap Size
-xx:newsize=n: Setting the young generation size
-xx:newratio=n: Sets the ratio of the younger generation to the older generation. such as: 3, the ratio of the young generation and the old generation is 1:3, the young generation of the entire young generation of old generation and 1/4
-xx:survivorratio=n: The ratio of Eden in the young generation to the two survivor districts. Note that there are two survivor districts. such as: 3, indicating Eden:survivor=3:2, a Survivor area accounted for the entire young generation of 1/5
-xx:maxpermsize=n: Setting the persistent generation size
Collector Settings
-XX:+USESERIALGC: Setting up the serial collector
-XX:+USEPARALLELGC: Setting up a parallel collector
-XX:+USEPARALLEDLOLDGC: Setting up a parallel old generation collector
-XX:+USECONCMARKSWEEPGC: Setting the concurrency Collector
Garbage collection Statistics
-XX:+PRINTHEAPATGC GC's heap details
-xx:+printgcdetails GC Details
-xx:+printgctimestamps Printing GC Time information
-xx:+printtenuringdistribution Print age information-xx:+handlepromotionfailure old age distribution guarantee (TRUE or false) parallel collector settings
-xx:parallelgcthreads=n: Sets the number of CPUs to use when the parallel collector is collected. The number of parallel collection threads.
-xx:maxgcpausemillis=n: Set maximum pause time for parallel collection
-xx:gctimeratio=n: Sets the percentage of time that garbage collection takes to run the program. The formula is 1/(1+n)
Concurrent collector Settings
-xx:+cmsincrementalmode: Set to incremental mode. Applies to single CPU conditions.
-xx:parallelgcthreads=n: Set the concurrency collector the number of CPUs used by the young generation collection method for parallel collection. The number of parallel collection threads.

6. Example: Heap size setting

Scenario: Under Java_home, execute the following command under demo/jfc/swingset2/directory:

java-jar-xmn4m-xms16m-xmx16m Swingset2.jar

System output:

Exception in thread "Image fetcher 0" Java.lang.OutOfMemoryError:Java Heap space

Exception in Thread "Image fetcher 3" Java.lang.OutOfMemoryError:Java heap Space

Exception in Thread "Image fetcher 1" Java.lang.OutOfMemoryError:Java heap Space

Exception in Thread "Image fetcher 2" Java.lang.OutOfMemoryError:Java heap Space

Tuning: Set the-XMS and-XMX options to 32m, while-xmn is a-xmx value of 1/4.

Results: The java-jar–xmn8m–xms32m-xmx32m Swingset2.jar is executed and the system is running normally.

7. JVM Runtime data area (runtime):

Figure 3:JVM Run-time data area (i)

HEAP:JVM there is only one heap shared for all threads, and all class instances and arrays are created in the heap.

Method AREA:JVM has only one methods area that is shared for all threads. It stores the class structure, such as code that runs a constant pool, member and method data, and methods, constructs methods.

Java Stacks: Each JVM thread has a private stack.

PC REGISTERS:JVM can support running multiple threads at the same time, so each thread needs its own PC (program counter) register.

Native method Stacks: Saves the address of the Native approach to the zone.

Figure 4:JVM Runtime data area (ii)

The heap and method area are shared by all threads and have the same lifetime as the JVM, and Java Stacks, Pc registers, Native method Stacks are owned by each thread and have the same lifetime as the thread.

8. Common Memory leak Errors

Many developers have encountered Java.lang.OutOfMemoryError errors. This error is divided into two types: Java.lang.OutOfMemoryError:Java heap space and java.lang.OutOfMemoryError:PermGen space. The cause of this error can be a program problem or a JVM parameter configuration problem. For parameter problems, the former can be configured with the-XMS and-XMX parameters, which can be set by configuring-xx:permsize and-xx:maxpermsize.

9. References:

1. A Brief History of Garbage collection–

http://www-128.ibm.com/developerworks/java/library/j-jtp10283/

2. Garbage collection in the HotSpot jvm–

http://www-128.ibm.com/developerworks/java/library/j-jtp11253/

3. Tuning garbage Collection with the 5.0 JAVATM Virtual machine

Http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html

4. Diagnosing a GC problem–

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

5, http://www.precisejava.com

GC (minor) log
Full GC log-verbose:gc-xms20m-xmx20m-xmn10m-xx:survivorratio=8-xx:-handlepromotionfailure-xx:+printheapatgc-xx:+ Printgctimestamps
-xx:+printgcdetails-xx:+printtenuringdistribution

Java Virtual machine--new generation and old age GC

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.