Explore JVM and GC

Source: Internet
Author: User
Tags xms

1. Distribution of generations in the Java heap:

Figure 1: Distribution of generations in the Java heap

YOUNG: Mainly used to store new objects.

Old: Mainly stores memory objects with long life cycles in applications.

Permanent: indicates the permanent storage area of the memory, which stores information about the class and Meta. When the class is loaded, it is placed in the permgen space area. unlike the heap region where the instance is stored, GC (garbage collection) does not clean up permgen space during the main program running, so if your app loads a lot of classes, the permgen space error may occur.

 

2. What is the GC algorithm used by JVM?

Generation-based collection.

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

During GC collection, young areas with short lifecycles are frequently collected );

A small area with a long collection lifecycle (old area );

Perm area ).

 

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

GC (or minor GC): the young area that collects short lifecycles ).

Full GC (or major GC): Collects young areas and old areas with short lifecycles ).

Their collection algorithms are different, so the time used varies. GC efficiency is also relatively high. We should minimize the number of full GC times. When system. GC () is called, GC does a full collection (both young generation and tenured generation ).

 

4. After minor GC, is Eden empty?

Yes, minor GC will move all the live objects in Eden to the same vor region. If the remaining live objects cannot be placed in the same region, they will be moved to the 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: Set the young generation size
-XX: newratio = N: Ratio of the young generation to the old generation. For example, if the value is 3, the ratio of the young generation to the old generation is. The young generation accounts for 1/4 of the young generation and the old generation.
-XX: Ratio vorratio = N: Ratio of the Eden zone in the young generation to the two region vor zones. Note that there are two vor zones. For example, 3 indicates EDEN: Primary vor =. One primary vor zone accounts for 1/5 of the young generation.

-XX: maxpermsize = N: sets the persistent generation size.
Collector settings
-XX: + useserialgc: sets the serial collector.
-XX: + useparallelgc: set parallel collectors
-XX: + useparalledloldgc: sets the parallel elder generation collector.
-XX: + useconcmarksweepgc: sets the concurrent collector.
Garbage collection statistics
-XX: + printgc
-XX: + printgcdetails
-XX: + printgctimestamps
-Xloggc: Filename
Parallel collector settings
-XX: parallelgcthreads = N: set the number of CPUs used for parallel collector collection. Number of parallel collection threads.
-XX: maxgcpausemillis = N: sets the maximum pause time for parallel collection.
-XX: gctimeratio = N: Set the percentage of the garbage collection time to the running time. The formula is 1/(1 + n)
Concurrent collector settings
-XX: + cmsincrementalmode: Set to incremental mode. Applicable to a single CPU.
-XX: parallelgcthreads = N: set the number of CPUs used when the concurrent collector is used for collecting data in parallel in the young generation. Number of parallel collection threads.

 

6. Example: heap size setting

Scenario: run the following command in the demo/jfc/swingset2/directory of java_home:

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

Optimization: Set the-XMS and-xmx options to 32 m, and-xmn to the-xmx value of 1/4.

Result: The system runs properly by running Java-jar-xmn8m-xms32m-xmx32m swingset2.jar.

 

7. JVM runtime data area (runtime data area ):

Figure 3: JVM runtime data zone (1)

Heap: The JVM only has one heap shared by all threads. All class instances and arrays are created in the heap.

Method Area: The JVM only has one method area shared by all threads. It stores class structures, such as runtime constant pool, member and method data, and Code of methods and constructor methods.

Java stacks: Each JVM thread has a private stack.

PC registers: JVM supports running multiple threads at the same time. Therefore, each thread needs its own PC (program counter) Register.

Native method stacks: the address for saving the native method to enter the region.

 

Figure 4: JVM runtime data zone (2)

Heap and method area are shared by all threads. Their lifetime is the same as that of JVM. Java stacks, PC registers, and Native method stacks are owned by each thread, and their lifetime is the same as that of the thread.

 

 

 

 

8. Common Memory leakage errors

Many developers have encountered java. Lang. outofmemoryerror errors. There are two types of errors: Java. Lang. outofmemoryerror: Java heap space and Java. Lang. outofmemoryerror: permgen space. This error may be caused by program problems or JVM parameter configuration problems. If the parameter is incorrect, the former can be set by configuring the-XMS and-xmx parameters, while the latter 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

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.