The automatic memory management mechanism advocated in Java ultimately boils down to the automated solution of two problems: allocating memory to objects and reclaiming memory allocated to objects. We’ve covered the mechanics of memory reclamation in detail in previous blogs, so let’s look at allocating memory to objects.

Generally speaking, the memory allocation of objects is allocated on the heap. Objects are mainly allocated in the Eden area of the new generation, and in a few cases, they are directly allocated in the old age. The rules for allocation are not 100 percent fixed, and the details depend on which combination of garbage collectors is currently used, as well as the memory-related parameter configuration in the virtual machine.

Objects are allocated in Eden area first

In most cases, objects are allocated in the Eden region of the new generation. When the Eden area does not have enough space to allocate, the virtual machine fires a Minor GC, which uses the replication algorithm for garbage collection.

Big object goes straight to the old age

Large objects are Java objects that require a large amount of continuous memory. The most typical large objects are long strings and arrays. Large objects are bad news for virtual machine memory allocation, as it is common for large objects to trigger a garbage collection ahead of time in order to obtain enough contiguous memory to accommodate them when there is not much memory left.

How many “large” objects count as large objects? The VM provides a -xx: PretenureSizeThreshold parameter. Objects that are larger than this parameter enter the old era directly. The goal is to avoid a lot of object replication in the Eden region and between the two Survivor regions (the new generation uses the replication algorithm to reclaim memory).

Long-lived objects enter the old age

Since virtual machines use generational collection algorithms to reclaim memory, memory collection must identify which objects should be placed in the new generation and which objects should be placed in the old generation. To do this, the virtual machine defines an age counter for each object, and the age value is stored in the object’s header. If the object survives after Eden’s birth and after the first Minor GC and can be accommodated by a Survivor zone, it is moved to a Survivor zone and its age is set to 1. Each time an object survives a Minor GC in a Survivor zone, its age increases by one year, and when it reaches a certain age (15 by default), it is moved to the old age. The age threshold for the object to be promoted to the old age can be set with the parameter -xx: MaxTenuringThreshold.

By the way, virtual machines don’t always require objects to be older than MaxTenuringThreshold. If the sum of all objects of the same age in a Survivor space is greater than half of the size in a Survivor space, All objects with an age value greater than or equal to this age can go directly to the old age without waiting until the age required by the MaxTenuringThreashold parameter.

Three things to watch ❤️

If you find this article helpful, I’d like to invite you to do three small favors for me:

  1. Like, forward, have your “like and comment”, is the motivation of my creation.

  2. Follow the public account “Java rotten pigskin” and share original knowledge from time to time.

  3. Also look forward to the follow-up article ing🚀

  4. [666] Scan the code to obtain the learning materials package

Author: Wind program ape

Source: www.cnblogs.com/fangfuhai/p…