1024programmer News How JavaGC works

How JavaGC works

Reprinted to : https://blog.csdn.net/d6619309/article/details/53358250

It is well known that , Java programs do not need to be like C++ The program handles the recovery and release of memory by itself in the program. This is because Java adds a garbage collection (GC) mechanism on the JVM virtual machine , to trigger garbage collection at the right time , recycle and release unnecessary memory space , to avoid unlimited OOM caused by memory growth. As a qualified Java programmer,It is necessary to understand Java GC related knowledge. On the one hand, mastering GC knowledge can help us quickly troubleshoot online problems caused by JVM, on the other hand, it can also help us properly tune the JVM before the release of Java applications,improve the execution efficiency of applications, reliability and robustness.

This article will introduce the following points:

  • Java heap memory structure
  • Generational recovery algorithm
  • Garbage collector
  • GC log
  • JVM parameter usage

The virtual machine discussed in this article is the default The HotSpot virtual machine used by the JDK.

1. Java heap memory structure

In order to understand the knowledge points in the following chapters, we should First have a certain understanding of the Java heap memory structure division. Java divides the heap memory into 3 parts : new generation, old generation and permanent generation , among which the new generation is further divided into three areas: Eden, S0, and S1 (Survivor). The structure is shown in the figure below :

+--------------------------- +--------------------------------+----------- --------+
| | | | | |
| Eden | S0 | S1 | Old generation | Perm |
| 43;--------------------------&# 43;----------------- --------------+-------------------+
||

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

The new objects we create in the program are generally in the Eden area of ​​the new generation Allocation space , If the survival time is long enough, it will enter the Survivor area , And if the survival time is longer , will be promoted and allocated to the old generation. The persistent generation stores Class metadata, method descriptions, etc.

  1. S0 and S1 are two areas of equal size , Allocating memory space will only be done in one of them , The other space is used to assist new students The new generation performs garbage collection,because the garbage collection strategy of the new generation is based on the replication algorithm,the idea is to use the Eden area and one of the two Survivor areas,such as the objects that need to survive in the S0 area Copy it to another empty Survivor area , such as S1 area , and then you can recycle the dead objects in Eden and S0 areas. In the next recovery, the roles of S0 and S1 will be reversed , S1 is used to store surviving objects and S0 is used to assist in garbage collection, so that it can be recycled.
  2. Some articles do not include the permanent generation into the Java heap memory. In fact, the permanent generation is what we call the method area, and the method area is often called Non-Heap (non-heap). It is only in the implementation of the HotSpot virtual machine that the GC generational collection is extended to the method area, or the permanent generation is used to implement the method area, for other virtual machines, the concept of permanent generation does not exist.
  3. Not all object creation will allocate memory space in the Eden area. For the Serial and ParNew garbage collectors, by specifying -XX:PretenureSizeThreshold={size} to set objects exceeding this threshold size directly into the old generation.

2. Generational collection algorithm

We generally discuss garbage collection Mainly for the new generation and the old generation in the Java heap memory,It is also because of the structural differences between the new generation and the old generation,so a generational recovery algorithm,that is, the garbage collection of the new generation and Old generation garbage collection uses a different collection algorithm. For the young generation,mainly use the copy algorithm,for the old generation,usually use the mark-clear algorithm or mark-sort Algorithm for recycling.

2.1 Replication Algorithm

The idea of ​​the replication algorithm is to divide the memory into two areas of equal size,use one of them each time. When this block of memory runs out,copy the surviving object to another area,and then perform memory recovery on this block. An example diagram is shown below:

Diagram of Copy Algorithm

This algorithm is simple,and relatively efficient,but the cost is that half of the memory space needs to be sacrificed for copying. Studies have shown that & # xff0c; 98% of the objects in the new generation have a very short lifetime & # xff0c; so it is not necessary to divide the entire new generation at a ratio of 1:1 & # xff0c; the usual practice is to divide the memory space of the new generation Divided into a larger Eden area and twong>-XX:+UseConcMarkSweepGC

After using this setting , the virtual machine will use ParNew + CMS + Serial Old Collector combination for garbage collection. Note that the Serial Old collector will be recycled as a backup collector after the Concurrent Mode Failure of the CMS collector fails (the memory will be defragmented).

  • -XX:+UseParallelGC

The default value for the virtual machine to run in server mode. With this setting & # xff0c; the virtual machine will use a collector combination of Parallel Scavenge & # 43; Serial Old (PS MarkSweep) for garbage collection.

  • -XX:+UseParallelOldGC

After using this setting , the virtual machine will use Parallel Scavengen + Parallel Old’s collector combination for garbage collection.

  • -XX:PretenureSizeThreshold

Set the object size directly promoted to the old age,objects larger than this parameter , will be allocated directly in the old generation instead of in the new generation. Note that this value can only be set to bytes , such as -XX:PretenureSizeThreshold=3145728 means that objects exceeding 3M will be directly allocated in the old age.

  • -XX:MaxTenuringThreshold

Set the age of the object promoted to the old generation. After each object has persisted in a Minor GC, its age will be increased by 1. When this value is exceeded, it will enter the old age. The default setting is -XX:MaxTenuringThreshold=15.

  • -XX:ParellelGCThreads

Set the number of threads for memory recovery during parallel GC. Only when the garbage collector used is multi-threaded mode,including ParNew, Parallel Scavenge, Parallel Old, CMS, the setting of this parameter will be effective.

  • -XX:CMSInitiatingOccupancyFraction

Set the CMS collector to trigger garbage collection after the old age space is used (percentage) . The default setting -XX:CMSInitiatingOccupancyFraction=68 indicates that CMS garbage collection will be triggered when the space usage ratio of the old generation reaches 68%. This parameter is valid only when the old generation collector is set to CMS.

  • -XX:+UseCMSCompactAtFullCollection

Set whether the CMS collector should perform a memory after garbage collection defragmentation. This parameter is valid only when the old generation collector is set to CMS.

  • -XX:CMSFullGCsBeforeCompaction

Set how many garbage collections the CMS collector performs before performing memory defragmentation. For example, setting -XX:CMSFullGCsBeforeCompaction=2 indicates that the CMS collector performs a memory defragmentation after 2 garbage collections. This parameter is valid only when the old generation collector is set to CMS.

  • -XX:+PrintGCDetails

indicates the details of the output GC.

  • -XX:+PrintGCDateStamps

Specify the time format when outputting GC , code>-XX:+PrintGCTimeStamps is more readable.

  • -Xloggc

Specify the storage location of the gc log. For example, -Xloggc:/var/log/myapp-gc.log means to save the gc log in the disk /var/log/ directory , the file name is myapp-gc.log.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-javagc-works/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索