Open In App

How Many Types of Memory Areas are Allocated by JVM?

Last Updated : 03 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

JVM (Java Virtual Machine) is an abstract machine. In other words, it is a program/software that takes Java bytecode and converts the bytecode (line by line) into machine-understandable code. JVM acts as a run-time engine to run Java applications. JVM is the one that calls the main method present in Java code. JVM is a part of the JRE(Java Runtime Environment). 

Operations Performed by JVM

The various operations performed by the JVM are listed below:

  • Loading of code: The JVM loads class files that contain Java bytecode into memory. This means it finds the necessary files and gets them ready to run.
  • Verification of code: The JVM checks the bytecode it has loaded to make sure it's correct and safe.
  • Executing the code: The JVM runs the verified bytecode by turning it into machine code that the computer can understand.
  • Providing a Runtime Environment: The JVM provides a runtime environment that helps manage system resources, memory, and garbage collection.

ClassLoader Subsystem

The ClassLoader is a subsystem of the JVM responsible for loading class files. It primarily performs three activities:

  • Loading: Locates and loads the class files into memory.
  • Linking: Combines the class files and resolves dependencies.
  • Initialization: Initializes static variables and executes static blocks.

Types of Memory Areas Allocated By JVM

The JVM allocates memory into 5 distinct areas to carry out its operations. These areas are:

  • Class(Method) Area
  • Heap
  • Stack
  • Program Counter Register (PC Register)
  • Native Method Stack

The image below demonstrates the types of memory areas allocated by JVM:

JVM Architecture


1. Class (Method) Area

The Class (Method) Area is a memory block in the JVM that stores important information about classes. It includes:

  • Class Code: The bytecode that defines the class itself.
  • Variable Code: This includes static variables and runtime constants associated with the class.
  • Method Code: The bytecode for all the methods defined in the class, including constructors.

Note: In Java 8 and later versions, the concept of the Method Area still exists but works differently now. Instead of being part of the heap memory, the class information like static variables and method details are stored in a separate space called Metaspace. This Metaspace lives outside the heap, using native system memory. So, static data and class metadata don’t take up space in the heap anymore.

The class area stores class-level data of every class such as the runtime constant pool, field and method data and the code for methods, enabling the JVM to execute Java programs effectively.

2. Heap

The Heap Area is the memory block in the JVM where all objects are created and stored. It is used to allocate memory for:

  • Objects: Instances of classes created during the execution of a program.
  • Class Interfaces: Memory for class interfaces is also allocated here.
  • Arrays: Since arrays are considered objects in Java, their memory is allocated in the heap as well.

Note: Static variables and methods are not stored in the heap from Java 8 onwards, instead they stored in the metaspace. The heap stores instance data like objects and array.

The Heap Area is crucial for dynamic memory allocation, allowing the JVM to allocate memory for objects at runtime.

3. Stack 

In Java, each thread has its own stack called the Run-Time Stack, created when the thread starts.

  • IT stores various things like method call frames, local varaibles, method parameters and return addresses.
  • The JVM only performs two operations directly on the stacks that is push(when a method is called) and pop(when a method execution completes).
  • Every method call performed by the thread is stored in the corresponding run-time stack. The stack area is used to store Method call information, Local variables, Method parameters, Return Address
  • After completing all method calls the stack becomes empty and that empty stack is destroyed by the JVM just before terminating the thread.

4. Program Counter Register

Each JVM thread has its own PC register that stores the address of the JVM instruction currently being executed.

  • For non-native method the PC hold address of the available JVM instruction.
  • For native method, the PC register value is undefined.

Note: PC register is capable of storing the return address or a native pointer on some specific platform.

5. Native Method Stack

It is also known as C stack and this area is used to store the state of native methods which are written in languages other than Java (for e.g C or C++).

  • Each thread has its own native method stack, which can be fixed or dynamically sized.
  • This stack handles native method calls and execution.

Next Article
Article Tags :
Practice Tags :

Similar Reads