How does the JVM allocate memory to objects?

When creating an object, you need to allocate memory for the new object, and allocating memory is allocating on the heap. When allocating on the heap, it may be allocated in the Eden area of ​​the new generation or in the old generation. The specific allocation strategy needs to refer to some memory allocation rules.

 

Priority is allocated in the Eden area

In most cases, new objects are allocated in the Eden area of ​​the new generation. When there is not enough space in the Eden area of ​​the new generation, a Minor GC will be triggered.

 

Large objects go directly to the old age

When a new object needs a lot of contiguous space and the space required by the object is greater than the -XX:PretenureSizeThreshold parameter value , the object will allocate memory space in the old generation, which can avoid a lot of memory copying in the new generation .

 

Older surviving objects enter the old age

Every time the young generation passes through a Minor GC, the age of the surviving object will increase by one year. When the age exceeds the value of the -XX:MaxTenuringThreshold parameter, the object will enter the old generation.

 

Dynamic object age judgment

If the sum of the size of all objects of the same age in the Survivor space of the new generation is greater than half of the Survivor space, objects whose age is equal to or older than this age will directly enter the old generation, and no longer wait for the condition that the age exceeds the value of the -XX:MaxTenuringThreshold parameter to enter the old generation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326038833&siteId=291194637