Skip to content

Java Memory Tuning

Rajendra Prasad Reddy Penumalli edited this page May 13, 2020 · 26 revisions

Java Application Memory Related Issues

  • Memory Leaks
  • Over Memory Consumption
  • High Allocation Rates

Tools for above Problems

  • 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

Memory Analysis Tool Kits

  1. Inspecting JVM
  2. Heap Dump : Snapshot of memory used by JVM
  3. Memory Profiling

1. Inspecting JVM

  1. jps
    • List all the Java processes
    • CLI tool
    • Ships with jvm
  2. Alternative
    • ps -ef | grep 'java'
  3. JvisualVM
    • GUI and CLI tool

Object Histograms

  • 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

Analyzing Heap Dump

  • 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.

JVM Statistics With jstat

  • jstat is used "to monitor Java Virtual Machine (JVM) statistics
  • Monitoring Java garbage collection with jstat

References:

Clone this wiki locally