0

How do we find the size of all instances of a class in JVM? Interested in a general programmatic solution that can be used as a library and not use jstat or reference-counting that requires modifying the original source code.

        List<ReferenceType> classes = vm.classesByName(klass.getName());
        List<ObjectReference> objectReferences = classes.get(0).instances(Long.MAX_VALUE);

Using the code above gets me all the ObjectReference(s) to the objects of a particular class. Ideally, we should be able to get the object from the object-reference and invoke VM.current().sizeOf(Object obj) using JOL (https://github.com/openjdk/jol). Once I iterate through all object references, I'll get the total size occupied by objects of the class. However, I found no documentation for obtaining an Object from its ObjectReference.

Seems like both Eclipse (https://help.eclipse.org/2022-12/index.jsp) and IntelliJ IDEA (https://www.jetbrains.com/help/idea/analyze-objects-in-the-jvm-heap.html) can get objects and compute 'retained size' for any classes. I wasn't able to discover how these IDEs implement this functionality.

NOLFXceptMe
  • 232
  • 1
  • 10
  • 2
    The code you have posted, uses the debugging API. It is not intended to run on the same JVM as the debugged program, hence, there should be no surprise that there is no way to get from the debug handle to the actual object. – Holger Jan 20 '23 at 08:13
  • How do Eclipse or IDEA get the actual instance object and its retained size? I'm okay with getting the shallow size, not the deep size of an object. – NOLFXceptMe Jan 21 '23 at 04:18
  • Eclipse/IDEA never get the actual object because it is in a different JVM, they just use the debug APIs to talk to the other JVM. Where in Eclipse are you seeing information about the object size? – greg-449 Jan 21 '23 at 08:46
  • 1
    @NOLFXceptMe letting the special case of JOL aside, there are only the two ways used by common tools, heap dumps or the debugging API. Neither allows to get to the actual runtime object. – Holger Jan 23 '23 at 07:32
  • @greg-449 For IDEA it is very clear how to get the object size while debugging a JVM application - https://www.jetbrains.com/help/idea/analyze-objects-in-the-jvm-heap.html – NOLFXceptMe Jan 25 '23 at 08:29
  • @Holger If I have an `ObjectReference` and not the object, is it still possible to get the object size, like IDEA does? – NOLFXceptMe Jan 25 '23 at 08:30
  • Seems like I am wrong about Eclipse showing object sizes. During debugging it only shows all instances - https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fshared%2Fref-allinstances.htm Thanks for the correction. – NOLFXceptMe Jan 25 '23 at 08:34
  • That IDEA page also says you have to set the "Attach memory agent" option - so it is using something special to get this information. – greg-449 Jan 25 '23 at 08:38
  • 1
    @greg-449 the term “agent” suggests that it is using the Instrumentation API, i.e. [`Instrumentation.getObjectSize(java.lang.Object)`](https://docs.oracle.com/en/java/javase/17/docs/api/java.instrument/java/lang/instrument/Instrumentation.html#getObjectSize(java.lang.Object)). The Java Agent is running within the same JVM then and sending the results to the other program (IDEA) that [loaded the Agent via the Attach API](https://docs.oracle.com/en/java/javase/17/docs/api/jdk.attach/com/sun/tools/attach/VirtualMachine.html#loadAgent(java.lang.String,java.lang.String)). – Holger Jan 26 '23 at 08:25
  • 1
    @NOLFXceptMe the [native API has a function](https://docs.oracle.com/en/java/javase/17/docs/specs/jvmti.html#GetObjectSize) but I didn’t find an equivalent in the JDI API. – Holger Jan 26 '23 at 08:39

1 Answers1

1

In Java objects are generally allocated on the heap. So without modifying the application, you can take a heap dump and analyze it. There are questions with answers here on SO, such as

But rather than creating the solution from scratch and inside your application you might want to look into Application Performance Monitoring tools that can easily be attached to any application and thus are reusable. Here is a noncomplete list of such tools.

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • I want a programmatic way to do this. For example, to be used by a scheduled thread that prints object sizes for a set of classes every minute as they are created and destroyed. – NOLFXceptMe Jan 20 '23 at 06:45
  • 1
    It’s worth reading [What Heap Dumps Are Lying To You About](https://shipilev.net/blog/2014/heapdump-is-a-lie/), to understand why analyzing a heap dump only gives you an estimate, in the best case. – Holger Jan 20 '23 at 08:12
  • Thank you! Seems like JOL is the only "online" and correct way to get size estimates. – NOLFXceptMe Jan 21 '23 at 04:20