Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java GC: top object classes promoted (by size)?

Please let me know what is the best way to determine composition of young generation memory promoted to old generation, after each young GC event?

Ideally I would like to know class names which are responsible say, for 80% of heap in each "young gen -> old gen" promotion chunk;

Example: I have 600M young gen, each tenure promotes 6M; I want to know which objects compose this 6M.

Thank you.

like image 795
Andrei Pozolotin Avatar asked Feb 25 '10 16:02

Andrei Pozolotin


People also ask

What is GC and how it works in Java?

Java garbage collection is the process by which Java programs perform automatic memory management. Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for short. When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program.

What is GC in Java?

What is Java Garbage Collection? Java applications obtain objects in memory as needed. It is the task of garbage collection (GC) in the Java virtual machine (JVM) to automatically determine what memory is no longer being used by a Java application and to recycle this memory for other uses.

How GC decides if objects are live in Java?

To determine which objects are no longer in use, the JVM intermittently runs what is very aptly called a mark-and-sweep algorithm. As you might intuit, it's a straightforward, two-step process: The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive.

Which of the following is not correct about Java garbage collection model?

Runtime. gc() asks the garbage collector to run, but the garbage collector never makes any guarantees about when it will run or what unreachable objects it will free from memory. Option C is wrong.


1 Answers

There is no easy way to do this, however, I have recently been analyzing memory performance of large java apps, and can share some experience.

Here is how I found what objects are being promoted to old gen:

First you need to identify what objects are in "old/tenured" space. This is basically you standard java heap analysis. For this I recommend jmap. It is part of the sun jvm. run: jmap -dump:file=heap.hprof PID to get a heap dump. This will pause the jvm during the dump (~for 30 secs on a 2GB heap)

Now load the .prof file in Memory analyser (the best tool for this, hands down) I would spend a day playing with Memory analyzer to understand it, watch the screencam (needs a login, but worth it).

Now you will know what objects are in your heap.

Here is the trick: In the overview screen of Memory analyser, there is a link to: "Unreachable objects histogram". Now these objects are all to be collected during the next GC. But some are probably in eden, some in survivor and some in old.

Now, get some profiler with memory profiling capability, I prefer yourKit. Run your app with yourkit and record object allocation.

Run it and record object creation. Once you have a list of objects created use all three lists to get a picture of what is going on. Do what humans do best, see patterns.

  • What objects are created and are reachable. (Memory analyser)
  • Objects unreachable in heap (Memory analyser)
  • Objects created during a run (Profiler)

Another way to approach is YourKit generations view. You can take snapshots of your heap and compare what objects are still alive between snapshots. If you use this with visualgc you can determine how long an object must be alive to be promoted to old gen, and take snapshots at these intervals to see what objects are still alive.

Well, good luck. /JT

like image 186
JT. Avatar answered Sep 18 '22 16:09

JT.