? QA Design Gurus: Java Heap Terminology and internals

Sep 1, 2015

Java Heap Terminology and internals


A clear understanding on Heap memory usage allows testers to understand memory leaks in the products. There seems like a common misunderstanding on the JVM Heap and Non Heap memory, which intended me in writing this. 

The heap stores all of the objects created by your Java program. The heap's contents are monitored by the garbage collector, which frees memory from the heap when you stop using an object (i.e. when there are no more references to the object. This is in contrast with the stack, which stores primitive types like ints and chars, are typically local variables and function return values. These are not garbage collected.

The perm space refers to a special part of the memory (Non-Heap).The permanent generation is special because it holds meta-data describing user classes (classes that are not part of the Java language). Examples of such meta-data are objects describing classes and methods and they are stored in the Permanent Generation. Applications with large code-base can quickly fill up this segment of the heap which will cause OOM. After then, no application shall run on that machine effectively even after having a huge empty JVM.
Below picture gives the detailed split of jvm and what the parameters that refer the exact spaces split under heap and non heap memory.



The memory pools for serial garbage collection are the following.
  • Eden Space (heap): The pool from which memory is initially allocated for most objects.
  • Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space.
  • Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space.
  • Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.
  • Code Cache (non-heap): This used for compilation and storage of methods that have been compiled to native code by the JIT compile. In this way the Hotspot VM tries to choose the most appropriate way to trade-off the extra time it takes to compile code verses the extra time it take to execute interpreted code.
Java uses generational garbage collection. This means, if a new object comes to java heap, it gets stored in young generation under Eden space. If this object survives garbage collection (if there are still references to it) then it is further promoted to survivor space. If the object exists in survivor for some time, it is then moved to tenured (old generation). In short, it starts in the young generation (which itself is divided into multiple spaces - Eden and Survivor) and would eventually end up in the tenured generation if it survived long enough.

No comments:

Post a Comment