Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of garbage Collection Mechanism in JVM

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of garbage collection mechanism in JVM, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Partition of heap memory

Divided into three parts (the following nouns denote the same area):

The new generation, the new generation, the younger generation

Old age, old age, old age

Permanent zone, permanent generation

The purpose of zoning

The sole purpose is to optimize GC performance.

If there is no generation, all our objects are put together, and we need to scan all areas of the heap during GC. And many objects are "life and death", if the new objects created are placed in a certain place, when GC, the area of the "life and death" object will be recycled first, which will free up a lot of space.

I. garbage collection mechanism in new areas

The newborn area is divided into Eden area, Survivor0 area, Survivor1 area (also known as from area and to area).

The Eden area accounts for 80% of the memory space, and each Survivor occupies 10% of the memory space (for example, 800m for Eden and 100m for each Survivor)

1. The objects created at the beginning are assigned to the Eden area. When the Eden area is almost full, it will trigger the garbage collection Minor GC (using the replication algorithm for garbage collection)

After 2.Minor GC processing, the objects that are still alive in the Eden area will be transferred to one of the idle Survivor areas at one time. The Eden area is then emptied, and the objects created continue to be placed in the Eden area until the next time the Eden is filled again.

When the 3.Eden fills up again, the Minor GC starts again. After the cleanup (Minor cleans up the memory of the Eden area and the Survivor area), the surviving objects in the Eden area and the Survivor area where the objects exist (at this time, the from area) are transferred to another empty Survivor area (now the to area), and the Eden area and the Survivor area where the objects previously exist are cleared (now it becomes the to area, and "From" and "To" exchange their roles. The new "To" is the "From" before the last GC, and the new "From" is the "To" before the last GC. )

This is the process of the replication algorithm.

Always keep a Survivor area empty to provide replication algorithm garbage collection, but this area of memory only accounts for 10% of the whole block of memory, the other 90% memory can be used, courseware memory utilization is still quite high.

Second, when will you enter the elderly area? 1 enter the elderly area after 15 times of GC

By default, if an object in the newborn area is not recycled after 15 times of GC, it will be transferred to the elderly area.

It can be set by the JVM parameter "- XX:MaxTenuringThreshold", which defaults to 15.

2 judging the age of dynamic objects

This method does not have to wait to go through GC15 times.

If the total size of a batch of objects is greater than 50% of the current Survivor memory, then objects greater than or equal to the age of these objects will be transferred to the elderly area.

Example: suppose that both objects in the Survivor0 area experience three times of GC (age 3), and that the total size of the two objects is 50m, more than half the memory size of the Survivor0 area. At this time, all the objects older than 3 years old in Survivor0 area will be transferred to the elderly area.

The three major objects go directly into the old era.

There is a JVM parameter "- XX:PretenureSizeThreshold", which defaults to 0, which means that in any case, the object is assigned to the Eden area first.

If set to 1048576 bytes, that is 1m. It means that when the created object is greater than 1m, the object will be placed directly into the old area and will not pass through the new area at all.

The reason for this: large objects degrade performance when they go through the replication algorithm for GC.

Too many objects survive after 4 Minor GC can not be put into the Survivor area.

There are so many objects alive after Minor GC that there is no room in the Survivor area. At this point, all objects will be transferred directly to the elderly area.

III. Principle of guarantee for space allocation in elderly areas

Before performing each Minor GC, JVM checks to see if the memory space available in the old area is greater than the total size of all objects in the newborn area.

Reason: in extreme cases, after Minor GC, all objects in the Cenozoic generation survive, which will put all Cenozoic objects in the old area.

If the memory available in the aging area is larger than the total size of the new generation of objects, then you can safely execute Minor GC.

However, if the memory in the old area is less than the total size of the new area object, it will see if a parameter: "- XX:HandlePromotionFailure" is set to true. If true, proceed to the next judgment to see whether the available memory in the seniority area is greater than the average size of objects entering the seniority area after each Minor GC. If the available memory in the old age is less than the average size or the parameter is not set to true, it will directly trigger "Full GC", that is, garbage collection will be carried out in the old age, and then Minor GC will be carried out after making room, which is tantamount to a unified clean-up of the new and elderly areas.

There are three cases of progressive understanding:

1. If the Survivor area size of the surviving object after Minor GC, but

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report