Java memory management is the process by which the Java Virtual Machine (JVM) automatically handles the allocation and deallocation of memory. It uses a garbage collector to reclaim memory by removing unused objects, eliminating the need for manual memory management
JVM Memory Structure
JVM defines various runtime data areas used during the execution of a program. Some of the areas are created by the JVM, whereas some are created by the threads that are used in a program. However, the memory area created by the JVM is destroyed only when the JVM exits. The data areas of a thread are created during instantiation and destroyed when the thread exits. These areas include:
- Heap Area
- Method Area
- JVM Stacks
- Native Method Stacks
- Program Counter (PC) Registers
Java Virtual Machine (JVM) Memory Areas
The image below demonstrates the Java Memory Area parts:
jvm-memory1. Heap Area
- Heap is a shared runtime data area where objects and arrays are stored. It is created when the JVM starts.
- JVM allows user to adjust the heap size. When the new keyword is used the object is allocated in the heap and its reference is stored in the stack.
- There exists one and only one heap for a running JVM process.
Scanner sc = new Scanner(System.in)
Here, the Scanner object is stored in the heap and the reference sc is stored in the stack
Note: Garbage collection in heap area is mandatory.
2. Method Area
- Method area is a logical part of the heap and it is created when the JVM starts.
- Method area is used to store class-level information such as class structures, Method bytecode, Static variables, Constant pool, Interfaces.
- Method area can be of fixed or dynamic size depending on the system's configuration.
- Static variables in Java are stored in the Method Area.
- Garbage collection of the method area is not guaranteed and depends on JVM implementation.
Note: Method area is logically a part of heap, many JVM like HotSpot uses a separate space known as Metaspace outside the heap to store it.
Example: Java Program to demonstrate how java variables are stored in the different memory areas
Java
import java.io.*;
class Geeks {
// static variables are stored in the Method Area
static int v = 100;
// instance variables are stored in the Heap
int i = 10;
public void Display()
{
// local variables are stored in the Stack
int s = 20;
System.out.println(v);
System.out.println(s);
}
}
public class Main {
public static void main(String[] args) {
Geeks g = new Geeks();
// Calling the Display method
g.Display();
}
}
Note:
- Static variables are stored in the Method Area.
- Instance variables are stored in the Heap.
- Local Variables are stored Stack.
3. JVM Stacks
- A stack is created when a thread is created and the JVM stack is used to store method execution data, including local variables, method arguments and return addresses.
- Each Thread has its own stack, ensuring thread safety.
- Stacks size can be either fixed or dynamic and it can be set when the stack is created.
- The memory for stack needs not to be contiguous.
- Once a method completes execution, its associated stack frame is removed automatically.
4. Native Method Stacks
- Native method stack is also known as C stacks.
- Native method stacks are not written in Java language.
- This memory is allocated for each thread when it is created and can have either a fixed or dynamic size.
- Native method stacks handle the execution of native methods that interact with the Java code.
5. Program Counter (PC) Registers
- Each JVM thread has a Program Counter (PC) register.
- For non-native methods, it stores the address of the current JVM instruction.
- For native methods, the PC value is undefined.
- On some platforms, the PC can also store a return address or native pointer.
Heap vs Stack
Heap | Stack |
---|
Stores objects and instance variables. | Stores method calls and local variables. |
Larger but slower. | Smaller but faster. |
Random access. | LIFO (Last-In-First-Out). |
Allocation is manual (using 'new' keyword), but deallocation is automatic. | Allocation and deallocation both are automatic. |
Long-lived. | Short-lived (method duration). |
Shared among all threads (needs synchronization). | Each thread has its own stack (thread safe). |
Garbage Collector in java
The Garbage collector automatically removes the unused objects that are no longer needed. It runs in the background to free up memory.
- Garbage collector finds objects that are no longer needed by the program.
- It removes those unused objects to free up the memory and making space for new objects.
- Java uses generational garbage collection so that new objects are collected more frequently in the young generation than older objects which survive longer in the old generation, this helps improve efficiency.
- You can request garbage collection using System.gc(), but the JVM ultimately decides when it should run.
Note: You can request GC with System.gc() or Runtime.gc(), but JVM decides when it runs.
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java