-
Notifications
You must be signed in to change notification settings - Fork 1
Java Memory Tuning
Rajendra Prasad Reddy Penumalli edited this page May 13, 2020
·
26 revisions
- Memory Leaks
- Over Memory Consumption
- High Allocation Rates
- Object Histograms: brief snapshot of what is using memory
- Heap Dumps : In depth snapshot of what is using memory
- Allocation Profiling : continuous measurement of what is allocating memory
- Inspecting JVM
- Heap Dump : Snapshot of memory used by JVM
- Memory Profiling
-
jps
- List all the Java processes
- CLI tool
- Ships with jvm
- Alternative
- ps -ef | grep 'java'
-
JvisualVM
- GUI and CLI tool
- List of how mach memory is used by different types of objects
- Essentially it is Heap memory distribution among objects
- It is just a snapshot
- Collecting is very fast ,even on production
- Low granularity
- It wont Relation between the objects
- Tool : jmap
- Command To Capture Object Histogram
$ jmap -histo <pid>
- where pid: is the Java Process Id, whose histogram should be captured
-
Command To Capture Heapdump on demand
$ jmap -dump:live,file=<file-path> <pid> $jmap -dump:live,format=b,file=<file-path> <pid>
- where pid: is the Java Process Id, whose heap dump should be captured
- file-path: is the file path where heap dump will be written in to.
- Note: It’s quite important that you pass the “live” option in the command line. If this option is passed then only the live objects in the heap are dumped. If your application is running with a large memory size then typically it would take a long time to capture the heap dump and even longer time for the tools to parse them. “Live” objects are the one that has active memory references. For analyzing memory leaks just live objects are good enough.
-
Capturing HeapDumpOnOutOfMemoryError
- Capture Heap Dump when JVM experienced OutOfMemoryError by passing ‘-XX:+HeapDumpOnOutOfMemoryError’ system property.
- This property is quite an effective property that I would recommend all JVMs to have this property configured. Due to heat of the moment, sometimes Operations team might forget to capture heap dump.
- It’s extremely hard to diagnose any memory problems without heap dumps. This Property can be the savior of the day in such circumstances.
Sample Usage:
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/logs/heapdump
- Java Heap Analysis Tool : jhat
- jhat command parses a Java heap dump file and starts a web server.
- jhat enables you to browse heap dumps using your favorite web browser
- Starting jhat Tool
* Objects are usually listed very often:
* char[]
* java.lang.Object[]
* java.lang.Class
* java.lang.String
* byte[]
* java.util.HashMap
* java.util.HashMap$Entry
* short[]
* int[]
* java.util.ArrayList
* java.lang.Integer
jhat -J-Xmx -port
where
memory-size: JVM argument. memory size with which JVM needs to be launched
port-number: Port in which jhat web application can be accessed
file-path: Location of Heap dump file
Sample:
jhat -J-Xmx2g -port 7001 /opt/workspace/tier1app/artifacts/LeakingMap/heapDump-2.bin
- Note: It’s important that you pass appropriate -Xmx, otherwise jhat will result in OutOfMemoryError.
- If heap dump size is very large, then need to pass larger -Xmx argument size.
- jstat is used "to monitor Java Virtual Machine (JVM) statistics
- Monitoring Java garbage collection with jstat
- https://www.linkedin.com/pulse/java-memory-leak-quick-refresher-shivaram-thirunavukkarasu
- https://blog.codecentric.de/en/2011/08/create-and-understand-java-heapdumps-act-4/
- https://blog.gceasy.io/2015/08/14/how-to-capture-heap-dump-jmap/
- https://blog.gceasy.io/2015/08/28/jhat-heap-dump-analysis/
- http://blog.weetech.co/2015/04/learning-java-jstat.html
- http://www.herongyang.com/Java-Tools/jstat-gcutil-Garbage-Collection-Statistics.html
- https://gceasy.io/index.jsp#features