Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out where exact young/old gen is located in memory?

Recently I was able to get an object's address using sun.misc.Unsafe class.

And now I am trying to find programmatically an actual generation where my object is located. For this I want to know the starting and ending points of each generation. If Java (Oracle JVM) provides any tools to resolve this issue? I believe not, since even different GCs require different memory structures (e.g. G1), and this makes the task even more interesting :)

What I want to know here is just a couple of numbers representing the borders of generations in memory, like this:

   young gen: start point - 8501702198   
              end point   - 9601256348

Would love to hear even your craziest ideas about a black magic that allows to determine where different generation areas are placed in memory.

like image 205
Anton Kasianchuk Avatar asked Jun 02 '15 15:06

Anton Kasianchuk


People also ask

On what basis will you decide the young generation and old generation size for an application?

The optimal choice depends on the lifetime distribution of the objects allocated by the application. By default, the young generation size is controlled by the parameter NewRatio . For example, setting -XX:NewRatio=3 means that the ratio between the young and tenured generation is 1:3.

What is young generation and old generation?

Young Generation : the newly created objects are allocated to the young gen. Old Generation : If the new object requests for a larger heap space, it gets allocated directly into the old gen. Also objects which have survived a few GC cycles gets promoted to the old gen i.e long lived objects house in old gen.

What is young generation garbage collection?

The Young Generation is where all new objects are allocated and aged. When the young generation fills up, this causes a minor garbage collection. Minor collections can be optimized assuming a high object mortality rate. A young generation full of dead objects is collected very quickly.

How to move objects from young generation to old generation?

Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually, the old generation needs to be collected.

What is the old generation used for?

The Old Generation is used to store long surviving objects. Typically, a threshold is set for young generation object and when that age is met, the object gets moved to the old generation. Eventually, the old generation needs to be collected.

What is the young generation in Java?

From a high level, the young generation is where all new objects start out. Once they’re allocated in the Java code, they go specifically to this subsection called the eden space. Eventually, the eden space fills up with objects. At this point, a minor garbage collection event occurs.

How to find out how old you were in the past?

If you wanted to know how old you were when something happened in the past, simply enter the date of the event at the top part of the calculator & it will calculate how old you were then. Aging: a natural process every living organism goes through. For centuries, humans have been searching for the fountain of youth to reverse old age.


1 Answers

This is possible with HotSpot JVM though somehow complicated.

The key idea is to use VMStructs - the information about HotSpot internal constants and types embedded right into JVM shared library.

For example, ParallelScavengeHeap::_young_gen VM global variable contains the pointer to PSYoungGen structure that has _virtual_space member with the boundaries of Parallel collector's young generation. Similarly, GenCollectedHeap::_gch global points to the structure describing CMS collector generations.

I've made a proof-of-concept project to demonstrate the usage of VMStructs. It is pure Java, no extra libraries required, but it deeply relies on the undocumented JDK internals and may not work on all Java versions. I've tested this on JDK 8u40 and JDK 7u80 on Windows and Linux.

  • JVM.java - the code for reading VMStructs;
  • HeapInfo.java - the sample program to get addresses of Heap generations.
like image 163
apangin Avatar answered Nov 14 '22 21:11

apangin