Test the JAVA generation garbage collection mechanism and java generation garbage collection mechanism

Source: Internet
Author: User

Test the JAVA generation garbage collection mechanism and java generation garbage collection mechanism

Java generational collection Test
Test code:

Package juint. test; import java. util. hashMap; import java. util. map; public class JstatTest {@ SuppressWarnings ("static-access") public static void main (String [] args) {Map <String, Object> map = new HashMap <String, object> (); for (int I = 0; I <1000000; I ++) {try {map. put (String. valueOf (I), new Integer (I); System. out. println ("loop --" + new Integer (I); Thread. currentThread (). sleep (10); // here you can adjust it to, 50, 10} catch (InterruptedException e) {e. printStackTrace ();}}}}
Command usage
Jstat-gcutil 1300 1000 500


In this article, YGC corresponds to FullGC for many times. Note that there are only two vor regions, and multiple regions can be defined. The data is as follows:

S0 S1 E O P YGC YGCT FGC FGCT GCT

100.00 0.00 53.40 74.12 3.06 12 0.170 1 0.033 0.202

Here, 12 YGC is performed, that is, 12 times are copied in the same vor, and FGC is performed once. Every time YGC stores objects in the Old, the following data is supported:

S0 S1 E O P YGC YGCT FGC FGCT GCT

0.00 0.00 100.00 0.00 3.06 0 0.000 0.000 0.000
0.00 100.00 4.02 5.94 3.06 1 0.013 0 0.000

No s0 or s1 at the beginning. After a YGC operation, there is content in O. The experiment shows that each time an object is copied to S0 or S1, will add a long-lived object to the old object, and the FGC will be executed, not necessarily because the Old is full. In the first group of data, the FGC will be executed, however, the O zone only has 74.12 of the data

To view the memory of the java Virtual Machine, run the following command: java-stat.


The following is the principle, thanks to the http://jefferent.iteye.com/blog/1123677 here to facilitate viewing, you can also directly copy the url on his blog to view

Virtual machines are divided into three generations: Young Generation, Old Generation, and Permanent Generation ). The persistent generation mainly stores the class information of Java classes, which has little to do with Java objects to be collected by garbage collection. The division of the young and old generations has a great impact on garbage collection.

  Young generation:

All newly generated objects are first put in the young generation. The goal of the young generation is to quickly collect objects with short lifecycles as much as possible. The young generation is divided into three zones. One Eden zone and two vor zones (generally ). Most objects are generated in the Eden area. When the Eden zone is full, the surviving objects will be copied to the primary vor zone (one of the two). When the primary vor zone is full, the surviving objects in this region will be copied to another region vor. When the region VOR is full, the surviving objects will be copied from the first region vor, it will be copied as "Tenured )". Note that the two regions of the same vor are symmetric and irrelevant. Therefore, objects copied from Eden and copied from the previous vor may exist in the same region, only objects copied from the first vor region are copied to the old district. In addition, there is always a blank vor area. At the same time, according to the program requirements, the VOR area can be configured as multiple (more than two), which can increase the time for the object to exist in the young generation and reduce the possibility of being put into the old generation.

  Elder Generation:

Objects that are still alive after N garbage collection in the young generation will be put into the old generation. Therefore, it can be considered that objects with long lifecycles are stored in the old generation.

  Permanent generation:

Used to store static files. Currently, Java classes and methods are supported. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as Hibernate, in this case, you need to set up a large persistent storage space to store the classes added during the running process. The persistent generation size is set by-XX: MaxPermSize = <N>.

Under what circumstances will garbage collection be triggered:

Because the object is divided into generations, the garbage collection area and time are different. There are two types of GC: Scavenge GC and Full GC.

Scavenge GC

Generally, when a new object is generated and the Eden application fails, Scavenge GC is triggered to perform GC on the Eden region to clear non-surviving objects, and move the surviving objects to the same vor area. Then, sort out the two zones in the same vor. In this way, GC is performed on the Eden area of the young generation and will not affect the old generation. Because most objects start from the Eden area and the Eden area is not allocated much, GC in the Eden area is performed frequently. Therefore, it is generally necessary to use fast and efficient algorithms so that Eden can be idle as soon as possible.

Full GC

Organize the entire heap, including Young, Tenured, and Perm. Full GC is slower than Scavenge GC because it is necessary to recycle the entire GC, so the number of Full GC should be minimized. In the process of JVM optimization, a major part of the work is to adjust FullGC. Full GC may occur due to the following reasons:

  • Tenured is full
  • Perm is full
  • System. gc () is displayed and called
  • Dynamic change of Heap domain allocation policies after the last 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.