Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print java objects memory usage

In following question: Possible Spring Boot or Spring Security Memory Leak

The user prints the java objects as follows:

 num     #instances         #bytes  class name
----------------------------------------------
   1:        395984       32564344  [C
   2:        388697        9328728  java.lang.String
   3:         61258        5915088  [B
   4:        100297        4814256  java.util.HashMap
   5:         50892        4478496  org.apache.catalina.session.StandardSession
   6:         58774        3656824  [Ljava.util.HashMap$Node;
   7:         84773        3390920  java.util.TreeMap$Entry
   8:         51522        3339304  [Ljava.util.Hashtable$Entry;
   9:         51834        3317376  java.util.concurrent.ConcurrentHashMap
  10:        102111        3267552  java.util.HashMap$Node
  11:         96256        3080192  java.util.concurrent.ConcurrentHashMap$Node
  12:         24101        2754560  [Ljava.util.concurrent.ConcurrentHashMap$Node;
  13:         51472        2470656  java.util.Hashtable
  14:         55102        2204080  java.util.LinkedHashMap$Entry
  15:         83020        1992480  java.util.ArrayList
  16:         34353        1923768  java.util.LinkedHashMap
  17:         59156        1892992  org.springframework.boot.loader.util.AsciiBytes
  18:         29574        1656144  org.springframework.boot.loader.jar.JarEntryData
  19:         18029        1586552  java.lang.reflect.Method
  20:         28391        1562080  [Ljava.lang.Object;
  21:         37178        1487120  java.lang.ref.SoftReference
  22:         47648        1446600  [I
  23:         52337        1256088  java.lang.Long
  24:         26134        1254432  java.util.TreeMap
  25:         50904        1221696  java.beans.PropertyChangeSupport
  26:         11777        1214464  java.lang.Class
  27:         23748        1139904  org.springframework.security.oauth2.provider.OAuth2Request
  28:         35994         863856  java.util.Collections$UnmodifiableRandomAccessList
  29:         50904         814464  java.beans.PropertyChangeSupport$PropertyChangeListenerMap
  30:         50892         814272  org.apache.catalina.session.StandardSessionFacade
  31:         49748         795968  java.util.HashSet
  32:         24066         770112  java.util.Collections$UnmodifiableMap
  33:         23748         759936  org.springframework.security.oauth2.provider.OAuth2Authentication
  34:         23748         759936  org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails
  35:         26000         624000  javax.management.openmbean.CompositeDataSupport
  36:         12015         576664  [Ljava.lang.String;
  37:         16319         522208  com.sun.org.apache.xerces.internal.xni.QName
  38:         15288         489216  java.lang.ref.WeakReference
  39:         26448         423168  java.util.LinkedHashSet
  40:         26011         416176  java.util.TreeMap$KeySet

What command did the user use to print this info?

Btw, I have added following arguments to my java process.

-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:gc.log -verbose:gc

I hope I have phrased the question correctly.

like image 448
hrishikeshp19 Avatar asked Jun 23 '15 17:06

hrishikeshp19


People also ask

How much memory does a Java object use?

Object references consume 4 bytes. boolean and byte values consume 1 byte. short and char values consume 2 bytes. int and float values consume 4 bytes.

How do I see RAM usage in Java?

In Java, you can access memory statistics via the Runtime class as follows: Runtime rt = Runtime. getRuntime();

Are Java objects stored in RAM?

In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In C++, when we allocate the object using new(), the object is allocated on Heap, otherwise on Stack if not global or static.

How do you know what memory an object needs?

getsizeof() function can be done to find the storage size of a particular object that occupies some space in the memory. This function returns the size of the object in bytes. It takes at most two arguments i.e Object itself.


2 Answers

That is the output from jmap -histo. Possibly jmap -histo:live. You run it as an external tool and supply the pid of the JVM. It is provided in the bin directory of your jdk installation.

It is typically safe to run in production but you should be aware of that jmap -histo:live triggers a full GC, which is necessary to only show the live objects. jmap -histo does not trigger a GC.

jmap documentation

like image 107
K Erlandsson Avatar answered Sep 27 '22 18:09

K Erlandsson


Alternatively you can create a heap-dump. And then use the jmap utility to obtain a histogram from the heap dump. If you are using IBM's JDK you can use IBM's Memory Analyzer tool (MAT) to get the histogram

https://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/memleaks.html#gbywm http://www.ibm.com/developerworks/java/jdk/tools/memoryanalyzer/

like image 20
Paritosh Avatar answered Sep 27 '22 17:09

Paritosh