Types Of Memory Areas in Java

Priya Salvi
3 min readJan 30, 2024

--

In Java, the memory is divided into different areas, each serving a specific purpose. The main memory areas in Java include:

1. Heap Memory:

  • Purpose: This is the runtime data area where objects and instances are allocated.
  • Managed by: The Java Virtual Machine (JVM).
  • Lifetime: Objects are created on the heap and are accessible by the entire application.
  • Usage: Stores instance variables and objects created at runtime.

2. Stack Memory:

  • Purpose: Stores local variables and partial results for each thread in a program.
  • Managed by: The Java Virtual Machine (JVM).
  • Lifetime: Variables exist as long as the method is on the call stack.
  • Usage: Manages method invocation and local variables.

3. Method Area (Non-Heap Memory):

  • Purpose: Stores class-level data, including class and method information, static variables, and constant pool.
  • Managed by: The Java Virtual Machine (JVM).
  • Lifetime: Persists for the entire duration of the application.
  • Usage: Contains class metadata and static variables.

4. Program Counter (PC) Register:

  • Purpose: Holds the address of the Java Virtual Machine instruction currently being executed.
  • Managed by: The Java Virtual Machine (JVM).
  • Lifetime: Changes with each executed bytecode instruction.
  • Usage: Keeps track of the current position in the bytecode.

5. Native Method Stack:

  • Purpose: Contains native method information and data.
  • Managed by: The Java Virtual Machine (JVM).
  • Lifetime: Tied to the thread and created when a thread is started.
  • Usage: Supports native (non-Java) methods.

Example:

Let’s understand different memory areas in Java with an example:

public class MemoryAreasExample {

// Static variable stored in the Method Area
static int staticVariable = 10;

public static void main(String[] args) {
// Local variable stored in the Stack Memory
int localVar = 20;

// Creating an object, and the object itself is stored in the Heap Memory
MyClass obj = new MyClass("Example Object");

// Method invocation, and the method and local variables are stored in the Stack Memory
obj.printMessage("Hello, Java!");

// Static method invocation, and the method is stored in the Method Area
staticMethod();

// Creating another object, and the object itself is stored in the Heap Memory
MyClass anotherObj = new MyClass("Another Object");

// Local variable stored in the Stack Memory
int anotherLocalVar = 30;
}

// Static method stored in the Method Area
static void staticMethod() {
System.out.println("Inside staticMethod");
}
}

class MyClass {
// Instance variable stored in the Heap Memory (part of the object)
String message;

// Constructor stored in the Heap Memory
public MyClass(String message) {
this.message = message;
}

// Instance method stored in the Heap Memory (part of the object)
void printMessage(String additionalMessage) {
System.out.println(message + ": " + additionalMessage);
}
}

In this example:

  • staticVariable is stored in the Method Area.
  • localVar and anotherLocalVar are stored in the Stack Memory.
  • Objects of MyClass (obj and anotherObj) are created in the Heap Memory.
  • The printMessage method and the constructor of MyClass are stored in the Heap Memory.
  • The staticMethod is stored in the Method Area.

These memory areas work together to manage the runtime behavior of a Java application. Understanding their roles is crucial for effective memory management and optimization.

--

--

Priya Salvi

Software Engineer | Oracle Certified Java Associate | Neophile | Bibliophile