Can I programmatically find out which GC generator the instance lives in?

This issue is limited to generations of HotSpot . Is there a way to programmatically find out in which generation a particular instance lives. Data such as:

  • Young or old generation?
  • If young, who survived?
  • Inside TLAB? Which stream?

Any technique (e.g. BTrace , JVMTI ) works as long as I can do something like this:

Object x = new Object(); HotSpotGenerationInfo info = HotSpotGenerationUtil.getInfo(x); 

Beggars cannot be elections, but ideally I could also find out when an instance of interest was moving from one generation to another at the time it occurred (i.e., based on an event callback - I was not interested in the delay and overhead implied in the survey. )

Not interested in answers that simply say no without justification :)

+8
java garbage-collection jvm jvm-hotspot jvmti
source share
2 answers

As far as I know, you cannot directly query which memory pool is currently in the object. However, objects are moved to a different memory pool by running the garbage collection, and you can request the number of major / minor gc starts from the moment the VM starts using JMX. If you additionally pay attention to these counters when the object is created, you can restore whether the GC was from that time and from which pool the object is located.

+4
source share

There is an additional complication for the approach "counting the number of GCs since the creation of the object" - this does not take into account premature promotion of the object .

If the surviving spaces are basically too small, and the pressure in the memory from Eden (i.e. the life speed of the objects at least once) is high, then the objects will increase before they reach the full threshold of ownership.

In real-life examples, healthy apps will usually have non-zero percentages of premature promotion. In fact, a 0% premature promotion course is a really bad sign - it says that your surviving spaces are much, too big, and you are wasting a lot of memory.

+3
source share

Source: https://habr.com/ru/post/650181/


All Articles