Heap Dump

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

Leak Suspects Report

Java Memory Model

What is a JVM ?

Java Virtual Machine | Various Components of Java Virtual Machine
  • JVM – Java Virtual Machine
  • It is an abstract machine.
  • It is a specification that provides runtime environment in which java bytecode can be executed.
  • JVMs are available for many hardware and software platforms
  • However JVM is platform dependent.
  • Whenever we execute a Java program, a separate memory area is reserved for storing various parts of our application code which we roughly call JVM memory.
  • Without having a good understanding of how JVM actually consumes the memory and how garbage collector uses different parts of this volatile memory, we may miss some important considerations for better memory management, thus better performance.

It is:

  1. A specification 
    • where working of Java Virtual Machine is specified.
    • But implementation provider is independent to choose the algorithm.
  2. An implementation 
    • Its implementation is known as JRE (Java Runtime Environment).
  3. Runtime Instance 
    • Whenever you write java command on the command prompt to run the java class, an instance of JVM is created.

JVM,

  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment
JVM | Java Virtual Machine - Javatpoint

 What is Java Garbage collection ?

  •  Garbage collection is the mechanism used in Java to de-allocate unused memory.
  • In java, garbage means referenced objects.
  • Java applications obtain objects in memory as needed.
  • It is the task of garbage collection (GC) in the Java virtual machine (JVM) to automatically determine what memory is no longer being used by a Java application and to recycle this memory for other uses.

What is a Memory Model?

  • The memory model describes possible behaviors of a program.
  • It describes thread interaction with memory.
  • It Changes in the ordering of reads and writes can cause race conditions. 
  • This memory model provides sequential consistency for data race free programs

What is Java Memory Model?

  • Java Memory Model is the model that describes the behavior of memory in Java program.
  • It is a set of rules all JVMs have to follow to ensure correct working of our concurrent programs. 
  • it is specification which guarantees visibility of fields when you have reordered of instructions.

The java memory model specification specifies how and when a thread can see the value of a shared variable modified by another thread and how to access the shared variable when necessary

eg:

If you run the program in one JVM then you pick that program and pick it into another JVM, the program output is exactly the same.

Java Memory Model Structure

  • The Java Virtual Machine defines various run-time data areas that are used during execution of a program.
Java Memory Management - GeeksforGeeks