What is a heap?
- Heap space in Java is used for dynamic memory allocation for Java objects and JRE classes at the runtime.
- Whenever we create a Java object by creating an instance of a class, it is always placed in an area known as the heap.
- The heap gets created when the JVM starts up.
- It expands or shrinks during runtime to accommodate the objects created or destroyed in our application.
- When the heap becomes full, the garbage collection process is run to collect the objects that are not referenced anymore.
- SHALLOW HEAP:
- The amount of memory occupied by the object itself
- The actual memory consumed by one object.
- RETAINED HEAP:
- Amount of memory that will be freed when the particular object is garbage collected
- The amount of memory that an object can be reclaimed,including the memory occupied by direct and indirect references.
- normally we say it is a sum of shallow sizes of all objects in the retained set of that object.
Key Features of Java Heap Memory
- It’s accessed via complex memory management techniques that include,
- Young Generation
- Old or Tenured Generation
- Permanent Generation
- If heap space is full, Java throws java.lang.OutOfMemoryError
- Access to this memory is relatively slower than stack memory
- This memory, in contrast to stack, isn’t automatically deallocated.
- It needs Garbage Collector to free up unused objects so as to keep the efficiency of the memory usage
- Unlike stack, a heap isn’t threadsafe and needs to be guarded by properly synchronizing the code
Types of Applications where Heap Size Matters
- BigData analyzing
- NoSQL Databases
- Analytics
- eCommerce
Garbage Collection
- Garbage Collection is process of reclaiming the runtime unused memory automatically
- It is a form of automatic memory management
- Garbage collection will free up the memory of the object that doesn’t have any reference.
What is a heap dump?
- Heap dumps contain a snapshot of all the live objects that are being used by a running Java application on the Java heap.
- It contains information such as,
- what are the objects in memory?
- what values do they carry?
- what is their size?
- what other objects do they reference?
Heap dumps formats:
- Classic format
- human-readable format (ascii text)
- Portable Heap Dump (PHD) format
- default (binary)
Usage of heap dump analyzing
- The JVM software allocates memory for objects from the heap for all class instances and arrays.
- The garbage collector reclaims the heap memory when an object is no longer needed and there are no references to the object.
- By examining the heap you can locate where objects are created and find the references to those objects in the source.
- So this heap dumps are very useful to troubleshoot memory-leak problems and optimize memory usage in Java applications.
- Memory problems that can investigate using the heal dumps
- Memory leaks
- Garbage Collection problems
- java.lang.OutOfMemoryError
OutofMemory Error
- Can be occurred due to the memory size that we already allocated for the application is not enough.
- Because didn’t increase the heap size.
What we are looking for in a Heap dump is:
- Objects with high memory usage
- Object graph to identify objects of not releasing memory
- Reachable and unreachable objects
How to Take Heap Dump
- A JVM argument can be added to generate heap dump whenever an OutOfMemoryError occurs.
- The
-XX:+HeapDumpOnOutOfMemoryError
option can be added to generate a heap dump on OutOfMemoryError.
- The
- By default, the heap dump is created in a file called java_pid pid .hprof in the working directory of the VM, but we can set an alternative path using the JVM option
-XX:HeapDumpPath=path
. - Using a jmap tool available with JDK.
- The following command can be executed from the command line:
-
jmap -dump:format=b,file=heap.bin <pid>
“<pid>” can be replaced with the process id of the application
-
jmap -dump:live,format=b,file=heapDump2.hprof 102707
- live: if set it only prints objects which have active references and discards the ones that are ready to be garbage collected. This parameter is optional
- format=b: specifies that the dump file will be in binary format. If not set the result is the same
- file: the file where the dump will be written to
- pid: id of the Java process
jcmd 12587 GC.heap_dump /tmp/dump.hprof
As with jmap, the dump generated is in binary format.