Detailed analysis of JVM heap model and GC garbage collection in jdk5.0.

Source: Internet
Author: User
Tags xms
Document directory
  • Young (nursery): Young Generation
  • Old (Tenured): Age
  • Permanent: Permanent generation
  • Young (nursery): Young Generation
  • Old (Tenured): Age
  • Permanent: Permanent generation

Some time ago, another out of swap (out of swap sapce) occurred in the performance test of a project. The situation is similar to that in the previous online shop version, after a few days, the system will be automatically ready without any adjustments, and the previous OOM will no longer be reproduced! Although the problem is awkward, we also take this opportunity to repeat the JVM heap model and GC garbage algorithm;

Basic concepts heap

The memory managed by the JVM is called heap. The 32bit operating system has a 4G limitation. Generally, 2 GB for Windows, 3 GB for Linux, and 64bit for Linux.
The memory initially allocated by JVM is specified by-XMS. The default value is 1/64 of the physical memory but less than 1 GB.
The maximum memory allocated by JVM is specified by-xmx. The default value is 1/4 of the physical memory but less than 1 GB.
By default, when the free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-xmx can be specified by-XX: minheapfreeratio =.
By default, when the free heap memory is greater than 70%, the JVM will reduce the minimum heap limit to-XMS, which can be specified by-XX: maxheapfreeratio =.
Generally, the server sets-XMS and-xmx to be equal to each other to avoid adjusting the heap size after each GC. Therefore, the above two parameters are useless.

Generational/heap Model

Generational collection is a highlight of Java garbage collection. Based on the length of the object's lifecycle, the heap is divided into three generations: young, old, and permanent, different collection algorithms can be used based on the characteristics of different generations to foster strengths and circumvent weaknesses. See the following model diagram:

Young (nursery): Young Generation

Research shows that most objects are dying and dying. Therefore, the young generation adopts a replication collection algorithm during GC. For details about the algorithm, refer to the following description;
The default value of Young is 4 MB, which is about 1/15 as the heap memory increases. The JVM dynamically manages the size change as needed.
Young is divided into three areas, One Eden, where all new objects exist and two vor regions are used to implement the replication algorithm.
-XX: The newratio = parameter can be used to set the size ratio of young to old. The default value of-server is, but is it much lower than the ratio of young at startup? If you cannot trust the JVM, you can use-xmn to specify its size. We recommend that you set it to 1/4 of the total heap size.
-XX: Specify vorratio = You can set the ratio of Eden to vor. The default value is 32. When vio is too large, it will be a waste. If it is too small, it will cause some young objects to flee to the elderly area, causing anxiety in the elderly area. However, this parameter is not very important to performance.

Old (Tenured): Age

If the objects of the young generation can be collected several times, they will enter the elderly area. The elderly area uses the tag sorting algorithm. Because the objects in the elderly area are not so easy to die, it is necessary to repeatedly copy objects by using the replication algorithm, which is not cost-effective. We have to use the mark clearing algorithm, but the mark clearing algorithm is not easy, every time you traverse all objects in the area, there is still no free lunch.
-XX: maxtenuringthreshold = sets the number of times the young generation has been collected and moved into the old area. The default value of CMS is 0. After the first GC, it is transferred. You can use-XX: + printtenuringdistribution to view it.

Permanent: Permanent generation

The default value for loading basic data such as class information is 64 mb. If it is a service program with many classes, you need to increase the setting-XX: maxpermsize =; otherwise, fullgc () will occur when it is full () or out of memory. Note that frameworks like spring and hibernate that like the dynamic generation of AOP require more persistent generation memory. Generally, the persistent generation does not perform GC unless it is forcibly set through-XX: + cmsclassunloadingenabled-XX: + cmspermgensweepingenabled.

GC type

When each generation is full, collection will be automatically promoted. The conditions triggered by each collector are different. Of course, some parameters can also be set forcibly. There are two types:

  • Minor collection: GC scans and recycles young at a high frequency and adopts a replication algorithm.
  • Major collection: memory collection for both young and old, also called full GC; because of the cost relationship, the check for old is much less frequently than young, and the mark clearing/marking algorithm is used. You can call the code system. GC () to raise the major collection and disable it using-XX: + disableexplicitgc, or set it to CMS concurrency-XX: + explicitgcinvokesconcurrent.

The details are as follows:
Because the young generation has a large number of people coming in and out, the GC of the young generation is more frequent, but the scope is also the objects in the young generation, which are characterized by a small number of times, but fast, called minor collection. When the memory usage of the young generation reaches a threshold, the minor collection is triggered. Eden and a certain memory or space (from space) the surviving objects are moved to another empty vertex or space (to space), and the roles from space and to space are called. When an object is moved between two consumer vor spaces for a certain number of times (reaching the preset threshold value), it is old enough to stay in the old age. Of course, if your vor
When space is small enough to accommodate all live objects, some live objects will be directly promoted to the old generation.
Buckets or spaces can be seen as a buffer between Eden and the old generation. Through this buffer, we can check whether an object's life cycle is long enough, because some objects have escaped a minor collection, it does not indicate that the lifecycle is long enough. It may have crashed before the next minor collection. To a certain extent, this ensures that the objects entering the old generation are genuine, reduces the growth rate of space usage in the old generation, and reduces the GC frequency in the old generation.
When the memory usage of the old or permanent generation reaches a certain threshold value, a GC based on all generations is triggered at a time. Its features must be in a wide range (large volume ), the time consumed is relatively long (relatively slow), but the frequency is relatively low (with fewer times). This is called major collection ). Generally, the GC Algorithm for the young generation is used first, and then the GC Algorithm for the old and permanent generations is used.

Basic GC collection Algorithm
  • Copy(Copying): divides the heap into two identical spaces and accesses each associated active object from the root (threadlocal object, static object, copy all the active objects of space a to Space B, and then recycle the whole space a at a time.
    Because only active objects are accessed, the whole space is cleared after all active objects are copied, and no dead objects are accessed. Therefore, the cost of traversing the space is small, however, it requires a huge copy cost and a large amount of memory. See the following example:
  • Mark Clear(Mark-sweep): The Collector first accesses all active objects from the root and marks them as active objects. Then, traverse the entire memory area and recycle all unmarked objects. The cost of traversing the entire space is large. The pause time increases linearly with the size of the space, and there are a lot of fragments in the heap after sorting. See the following example:
  • Mark(Mark-sweep-compact): integrates the practices and advantages of the two, first marks the active object, and then combines it into a large memory block. See the following example:
GC collector type
  1. OldSerial collector(Serial collector)
    -XX: + useserialgc: Serial replication policy for young generation, serial marking for old generation. See the following example:
  2. Throughput-firstParallel collector(Throughput collector)
    -XX: + useparallelgc: This is the default value of JDK 5-server. Policy:
    Young Generation: suspend the application. Multiple garbage collection threads are used to concurrently copy and collect data. The number of threads is the number of CPUs by default. When there are many CPUs, the number of threads can be-XX: parallelgcthreads = set.
    Old Generation: Pause the application. Like the serial collector, the individual garbage collection thread marks the sorting.
    As shown above, the collector is superior to the serial collector when it requires 2 + CPUs. It is suitable for background processing and scientific computing.
    You can use-XX: maxgcpausemillis = and-XX: gctimeratio to adjust the GC time. See the following example:
  3. Suspend time firstConcurrent collector(Concurrent low pause collector-Cms)
    -XX: + useconcmarksweepgc: This is an upgraded version of the above two policies. The policy is:
    Young Generation: The application is also suspended, and multiple garbage collection threads are concurrently copying and collecting.
    Old Generation: only two short pauses are performed, and applications and collection threads are concurrently cleared at other times.
    To use the tag sorting algorithm, you can set parameters. See the following example:
  4. Incremental concurrency collector(Incremental Concurrent-mark-sweep/I-Cms): Although the CMS collection algorithm adopts multi-thread concurrent operations in the most time-consuming memory area, when the server CPU resources are insufficient, in fact, the performance is not improved, but it will lead to a decline in system throughput. To avoid this situation as much as possible, there will be an incremental CMS collection algorithm, this means that the GC thread and user thread can run at the same time during concurrent marking and cleaning to minimize the full-process exclusive execution of the GC thread. See the following example:

For detailed configuration parameters of the above GC collector, refer to the collection of JVM options for the complete collection of JVM options.

Differences between parallelism and concurrency

The difference between parallel and concurrent is only one word, but the meaning is completely different. This may be a bit confusing for many people, you can try to figure out the above sample GC collector;

  • Parallel: Multiple garbage collection threads run concurrently. At this time, the user thread does not run;
  • Concurrency: the user thread and the garbage collection thread run concurrently. The program continues to run, while the garbage collection program runs on another CPU.

At the beginning of concurrent collection, all threads will be stopped for a short time to start marking the root object, then marking the concurrent running of the thread and the application thread, and finally suspending it for a short time, re-mark the objects that may be missed due to concurrency in multi-thread parallel mode, and then start the concurrent Cleaning Process with the application. It can be seen that the longest two traversal processes are executed concurrently with the application, which is too much improved than the previous Serial Algorithm !!!
Serial mark clearing starts collection when the old generation is full. Concurrent collection is performed because it is run with the application. If the collection is full, the application has no memory available, therefore, the system starts collection when 68% is full by default. The memory has been set to a large size. When the memory consumption is not so fast, you can use-XX: cmsinitiatingoccupancyfraction = to increase the ratio appropriately.

The pain of the young generation

Because of the replication and collection of the young generation, all application threads must be stopped. In this case, the collection speed can be improved only by multiple CPUs and multiple collection threads concurrently, however, unless your server exclusively occupies the entire server, if there are many other threads on the server, the switching speed will be ..... so, at the end, the bottleneck of the pause time falls on the replication algorithm of the young generation.
ThereforeYoung's size setting is very important.When the GC interval is increased, the multi-point object can die without copying. However, when young increases, the pause time caused by GC is very scary. According to a person's test results, the default 8 M Young, only a few milliseconds, 64 m rose to 90 ms, when it reaches 300 m, it will take 800 ms, and the peak will reach Ms. Who is the replication algorithm? When young is full, the collection starts. When the collection starts, it is necessary to stop all threads.

References

For details, refer to: optimization of garbage collection in jdk5.0 -- Don't pause.
Official Guide: tuning garbage collection with the 5.0 Java Virtual Machine

Some time ago, another out of swap (out of swap sapce) occurred in the performance test of a project. The situation is similar to that in the previous online shop version, after a few days, the system will be automatically ready without any adjustments, and the previous OOM will no longer be reproduced! Although the problem is awkward, we also take this opportunity to repeat the JVM heap model and GC garbage algorithm;

Basic concepts heap

The memory managed by the JVM is called heap. The 32bit operating system has a 4G limitation. Generally, 2 GB for Windows, 3 GB for Linux, and 64bit for Linux.
The memory initially allocated by JVM is specified by-XMS. The default value is 1/64 of the physical memory but less than 1 GB.
The maximum memory allocated by JVM is specified by-xmx. The default value is 1/4 of the physical memory but less than 1 GB.
By default, when the free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-xmx can be specified by-XX: minheapfreeratio =.
By default, when the free heap memory is greater than 70%, the JVM will reduce the minimum heap limit to-XMS, which can be specified by-XX: maxheapfreeratio =.
Generally, the server sets-XMS and-xmx to be equal to each other to avoid adjusting the heap size after each GC. Therefore, the above two parameters are useless.

Generational/heap Model

Generational collection is a highlight of Java garbage collection. Based on the length of the object's lifecycle, the heap is divided into three generations: young, old, and permanent, different collection algorithms can be used based on the characteristics of different generations to foster strengths and circumvent weaknesses. See the following model diagram:

Young (nursery): Young Generation

Research shows that most objects are dying and dying. Therefore, the young generation adopts a replication collection algorithm during GC. For details about the algorithm, refer to the following description;
The default value of Young is 4 MB, which is about 1/15 as the heap memory increases. The JVM dynamically manages the size change as needed.
Young is divided into three areas, One Eden, where all new objects exist and two vor regions are used to implement the replication algorithm.
-XX: The newratio = parameter can be used to set the size ratio of young to old. The default value of-server is, but is it much lower than the ratio of young at startup? If you cannot trust the JVM, you can use-xmn to specify its size. We recommend that you set it to 1/4 of the total heap size.
-XX: Specify vorratio = You can set the ratio of Eden to vor. The default value is 32. When vio is too large, it will be a waste. If it is too small, it will cause some young objects to flee to the elderly area, causing anxiety in the elderly area. However, this parameter is not very important to performance.

Old (Tenured): Age

If the objects of the young generation can be collected several times, they will enter the elderly area. The elderly area uses the tag sorting algorithm. Because the objects in the elderly area are not so easy to die, it is necessary to repeatedly copy objects by using the replication algorithm, which is not cost-effective. We have to use the mark clearing algorithm, but the mark clearing algorithm is not easy, every time you traverse all objects in the area, there is still no free lunch.
-XX: maxtenuringthreshold = sets the number of times the young generation has been collected and moved into the old area. The default value of CMS is 0. After the first GC, it is transferred. You can use-XX: + printtenuringdistribution to view it.

Permanent: Permanent generation

The default value for loading basic data such as class information is 64 mb. If it is a service program with many classes, you need to increase the setting-XX: maxpermsize =; otherwise, fullgc () will occur when it is full () or out of memory. Note that frameworks like spring and hibernate that like the dynamic generation of AOP require more persistent generation memory. Generally, the persistent generation does not perform GC unless it is forcibly set through-XX: + cmsclassunloadingenabled-XX: + cmspermgensweepingenabled.

GC type

When each generation is full, collection will be automatically promoted. The conditions triggered by each collector are different. Of course, some parameters can also be set forcibly. There are two types:

  • Minor collection: GC scans and recycles young at a high frequency and adopts a replication algorithm.
  • Major collection: memory collection for both young and old, also called full GC; because of the cost relationship, the check for old is much less frequently than young, and the mark clearing/marking algorithm is used. You can call the code system. GC () to raise the major collection and disable it using-XX: + disableexplicitgc, or set it to CMS concurrency-XX: + explicitgcinvokesconcurrent.

The details are as follows:
Because the young generation has a large number of people coming in and out, the GC of the young generation is more frequent, but the scope is also the objects in the young generation, which are characterized by a small number of times, but fast, called minor collection. When the memory usage of the young generation reaches a threshold, the minor collection is triggered. Eden and a certain memory or space (from space) the surviving objects are moved to another empty vertex or space (to space), and the roles from space and to space are called. When an object is moved between two consumer vor spaces for a certain number of times (reaching the preset threshold value), it is old enough to stay in the old age. Of course, if your vor
When space is small enough to accommodate all live objects, some live objects will be directly promoted to the old generation.
Buckets or spaces can be seen as a buffer between Eden and the old generation. Through this buffer, we can check whether an object's life cycle is long enough, because some objects have escaped a minor collection, it does not indicate that the lifecycle is long enough. It may have crashed before the next minor collection. To a certain extent, this ensures that the objects entering the old generation are genuine, reduces the growth rate of space usage in the old generation, and reduces the GC frequency in the old generation.
When the memory usage of the old or permanent generation reaches a certain threshold value, a GC based on all generations is triggered at a time. Its features must be in a wide range (large volume ), the time consumed is relatively long (relatively slow), but the frequency is relatively low (with fewer times). This is called major collection ). Generally, the GC Algorithm for the young generation is used first, and then the GC Algorithm for the old and permanent generations is used.

Basic GC collection Algorithm
  • Copy(Copying): divides the heap into two identical spaces and accesses each associated active object from the root (threadlocal object, static object, copy all the active objects of space a to Space B, and then recycle the whole space a at a time.
    Because only active objects are accessed, the whole space is cleared after all active objects are copied, and no dead objects are accessed. Therefore, the cost of traversing the space is small, however, it requires a huge copy cost and a large amount of memory. See the following example:
  • Mark Clear(Mark-sweep): The Collector first accesses all active objects from the root and marks them as active objects. Then, traverse the entire memory area and recycle all unmarked objects. The cost of traversing the entire space is large. The pause time increases linearly with the size of the space, and there are a lot of fragments in the heap after sorting. See the following example:
  • Mark(Mark-sweep-compact): integrates the practices and advantages of the two, first marks the active object, and then combines it into a large memory block. See the following example:
GC collector type
  1. OldSerial collector(Serial collector)
    -XX: + useserialgc: Serial replication policy for young generation, serial marking for old generation. See the following example:
  2. Throughput-firstParallel collector(Throughput collector)
    -XX: + useparallelgc: This is the default value of JDK 5-server. Policy:
    Young Generation: suspend the application. Multiple garbage collection threads are used to concurrently copy and collect data. The number of threads is the number of CPUs by default. When there are many CPUs, the number of threads can be-XX: parallelgcthreads = set.
    Old Generation: Pause the application. Like the serial collector, the individual garbage collection thread marks the sorting.
    As shown above, the collector is superior to the serial collector when it requires 2 + CPUs. It is suitable for background processing and scientific computing.
    You can use-XX: maxgcpausemillis = and-XX: gctimeratio to adjust the GC time. See the following example:
  3. Suspend time firstConcurrent collector(Concurrent low pause collector-Cms)
    -XX: + useconcmarksweepgc: This is an upgraded version of the above two policies. The policy is:
    Young Generation: The application is also suspended, and multiple garbage collection threads are concurrently copying and collecting.
    Old Generation: only two short pauses are performed, and applications and collection threads are concurrently cleared at other times.
    To use the tag sorting algorithm, you can set parameters. See the following example:
  4. Incremental concurrency collector(Incremental Concurrent-mark-sweep/I-Cms): Although the CMS collection algorithm adopts multi-thread concurrent operations in the most time-consuming memory area, when the server CPU resources are insufficient, in fact, the performance is not improved, but it will lead to a decline in system throughput. To avoid this situation as much as possible, there will be an incremental CMS collection algorithm, this means that the GC thread and user thread can run at the same time during concurrent marking and cleaning to minimize the full-process exclusive execution of the GC thread. See the following example:

For detailed configuration parameters of the above GC collector, refer to the collection of JVM options for the complete collection of JVM options.

Differences between parallelism and concurrency

The difference between parallel and concurrent is only one word, but the meaning is completely different. This may be a bit confusing for many people, you can try to figure out the above sample GC collector;

  • Parallel: Multiple garbage collection threads run concurrently. At this time, the user thread does not run;
  • Concurrency: the user thread and the garbage collection thread run concurrently. The program continues to run, while the garbage collection program runs on another CPU.

At the beginning of concurrent collection, all threads will be stopped for a short time to start marking the root object, then marking the concurrent running of the thread and the application thread, and finally suspending it for a short time, re-mark the objects that may be missed due to concurrency in multi-thread parallel mode, and then start the concurrent Cleaning Process with the application. It can be seen that the longest two traversal processes are executed concurrently with the application, which is too much improved than the previous Serial Algorithm !!!
Serial mark clearing starts collection when the old generation is full. Concurrent collection is performed because it is run with the application. If the collection is full, the application has no memory available, therefore, the system starts collection when 68% is full by default. The memory has been set to a large size. When the memory consumption is not so fast, you can use-XX: cmsinitiatingoccupancyfraction = to increase the ratio appropriately.

The pain of the young generation

Because of the replication and collection of the young generation, all application threads must be stopped. In this case, the collection speed can be improved only by multiple CPUs and multiple collection threads concurrently, however, unless your server exclusively occupies the entire server, if there are many other threads on the server, the switching speed will be ..... so, at the end, the bottleneck of the pause time falls on the replication algorithm of the young generation.
ThereforeYoung's size setting is very important.When the GC interval is increased, the multi-point object can die without copying. However, when young increases, the pause time caused by GC is very scary. According to a person's test results, the default 8 M Young, only a few milliseconds, 64 m rose to 90 ms, when it reaches 300 m, it will take 800 ms, and the peak will reach Ms. Who is the replication algorithm? When young is full, the collection starts. When the collection starts, it is necessary to stop all threads.

References

For details, refer to: optimization of garbage collection in jdk5.0 -- Don't pause.
Official Guide: tuning garbage collection with the 5.0 Java Virtual Machine

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.