8

how do I find size of object in heap including all the objects it references from its fields?

In the Classes view under heap dump, the size only indicates the "shallow" size of object. I would like to know transitively what the size of the object is, basically the total size of objects that it references through fields.

thanks

Jack
  • 195
  • 2
  • 8

2 Answers2

10

Take a heap dump (right-click on your application in the left panel, then "Heap dump")

Double-click on the class of your choice in "Classes" tab and it will open the "instances" tab for this class.

Finally click on "Compute Retained Sizes" on top-right corner of the tab.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
Tomas Hurka
  • 6,723
  • 29
  • 38
  • 2
    How to reach "instances view" ? At first take a heap dump (right-click on your application in the left panel, then "Heap dump") then double-click on the class of your choice in "Classes" tab and it will open the "instances" tab for this class. Finally click on "Compute Retained Sizes" on top-right corner of the tab. – user1075613 Dec 20 '18 at 14:53
0

You can get Retained and Shallow size. Differences see there https://www.yourkit.com/docs/java/help/sizes.jsp
*) To get "Retained size" jvisulVM->the "monitor" tab->the "heap dump" tab->go to the created heapdump->click on it "OQL Console"-> and in the bottom of it "Query editor" write your OQL sql for example to find the String with the val "GHT" the query will be select s from java.lang.String s where s.toString().equals("GHT")->click "execute"->and in the query result click by needed reference->you can see there information about your instance and you can click there "Compute retained size" to get that you want
*) To get the "Shallow" size jvisulVM->the "monitor" tab->the "heap dump" tab->go to the created heapdump->click on it "OQL Console"-> and in the bottom of it "Query editor" write your OQL sql for example to find the String with the val "GHT" the query will be select sizeof(s) from java.lang.String s where s.toString().equals("GHT")->click "execute"->you will see your object size in bytes
More verbose you can read about it here "Shallow" https://blogs.oracle.com/sundararajan/querying-java-heap-with-oql