Java's memory management involves understanding how the Java Virtual Machine (JVM) allocates, uses, and deallocates memory. The two primary memory areas in the JVM are the stack and the heap.
Stack
The stack is a region of memory that is used for the execution of threads. It stores local variables and partial results and participates in method invocation and return. Here are key characteristics:
- Memory Allocation: Stack memory is organized in a Last-In-First-Out (LIFO) manner. Each time a method is called, a new block (called a stack frame) is created on top of the stack to hold local variables and partial results.
- Memory Deallocation: When a method finishes execution, its stack frame is removed, and control is returned to the calling method.
- Size: Stack memory is usually smaller than heap memory and is limited by the operating system.
- Lifetime: The lifetime of a stack variable is limited to the duration of the method that created it.
- Scope: Variables stored in the stack are only accessible within the method they are defined in.
Heap
The heap is a region of memory used for dynamic memory allocation. It stores objects and JRE (Java Runtime Environment) classes. Here are key characteristics:
- Memory Allocation: Memory in the heap is allocated for objects and arrays. It is managed by the garbage collector.
- Garbage Collection: The heap is managed by the garbage collector, which automatically reclaims memory used by objects that are no longer reachable from any live thread.
- Size: Heap memory is typically larger than stack memory and can grow dynamically as needed, limited by the JVM and system settings.
- Lifetime: Objects in the heap remain there until they are no longer referenced and garbage collected.
- Scope: Objects stored in the heap are globally accessible, as long as references to them exist.
Interaction between Stack and Heap
- When a new object is created, it is allocated on the heap, and a reference to this object is stored in the stack.
- Method calls and local variables are managed in the stack, but objects created within methods (like
new
keywords) are allocated in the heap. - The stack contains references to objects that are stored in the heap.
Example
public class MemoryManagement {
public static void main(String[] args) {
int x = 5; // 'x' is a local variable stored on the stack
MyObject obj = new MyObject(); // 'obj' is a reference stored on the stack,
// the actual object is in the heap
obj.doSomething();
}
}
class MyObject {
public void doSomething() {
int y = 10; // 'y' is a local variable stored on the stack
System.out.println(y);
}
}
- In the
main
method, the integer variablex
and the referenceobj
are stored on the stack. - The actual
MyObject
instance created bynew MyObject()
is stored in the heap. - When
doSomething
is called, a new stack frame is created for this method, where the local variabley
is stored.
Summary
- Stack: Used for method calls and local variables, has a LIFO structure, size is limited, and is managed by the JVM automatically.
- Heap: Used for dynamic memory allocation, size can grow, and managed by the garbage collector.
Read more
Explain the Collections in Java
Features of Java 9, 10, 11. What's New?
Explain the SOLID principle in Java programming
Leave Comment