Tuesday 2 February 2016

Basic Understanding of JVM

Java Virtual Machine (JVM) is an abstract computing machine that enables a computer to run a Java program. A run time instance of JVM run one application. When a Java application starts, a run time instance starts and ends/dies when the application dies/closes/completes. Java application runs inside its own JVM.

Java Runtime Environment (JRE) is composed of the Java API and the JVM. The role of the JVM is to read the Java application through the Classloader and execute it along with the Java API.


The JVM specification gives a lot of leeway to implementors regarding the implementation details. Since Java 1.3, JRE from Oracle contains a JVM called HotSpot. It has been designed to be a high-performance JVM.

To speed-up code execution, HotSpot relies on just-in-time compilation. To speed-up object allocation and garbage collection, HotSpot uses generational heap.

Generational heap

The Java virtual machine heap is the area of memory used by the JVM for dynamic memory allocation.
In HotSpot the heap is divided into generations:
  • The young generation stores short-lived objects that are created and immediately garbage collected.
  • Objects that persist longer are moved to the old generation (also called the tenured generation). This memory is subdivided into (two) Survivors spaces where the objects that survived the first and next garbage collections are stored.
The permanent generation (or permgen) was used for class definitions and associated metadata prior to Java 8. Permanent generation was not part of the heap.The permanent generation was removed from Java 8.

JVM Structure




JVM Responsibility


  • Loading, verifying, and executing the byte code
  • Providing a runtime environment for execution of bytecode
  • Memory management and garbage collection
Memory management and garbage collection: Lets discuss about the heap area.

Heap memory :  This is the region of memory where objects are stored. Any object, whose size cannot be determined during compilation time would be stored in Heap, i;e - Instances of Classes. Size of any standard object (belongs to fundamental data types) can be determined during compilation and placed in stack in memory.


Whenever a class or array is created in a running java application, memory is allocated in the single heap inside the JVM. Each application runs inside its own JVM and have respective heap area. All thread share the heap space inside one application. JVM has an instruction to allocate memory, but can't deallocate. The deallocation of memory is managed by JVM and the process is called Garbage Collection.


The heap memory contains Garbage Collected heap. This area of heap actually contains the objects. It deallocate memory when these objects are no longer required or referenced in the code.This process of memory deallocation is called Garbage Collection (GC). This GC works on the basis of Reference Counting and Mark & Sweep method.


Next chapter is on Memory Leaks


1 comment: