When does memory get allocated to Heap and Stack in Java?

In Java, memory is allocated to the heap and stack at different times, depending on the type of data and the way it is managed. Here’s a breakdown of when each memory area is used:

Stack Memory

The stack is a region of memory used for storing:

  • Local variables (primitives and object references) within methods.
  • Function call information, including method arguments, return values, and the state of variables during method execution.

Characteristics:

  • Memory Allocation: Happens when a method is called. Each method call creates a “stack frame” for its local variables and parameters.
  • Scope and Lifetime: Variables exist only within the scope of the method and are automatically deallocated when the method finishes executing.
  • Storage: Stores only references to objects (not the actual objects themselves) when it comes to non-primitive data.
  • Speed: Faster access compared to heap, as stack memory is organized and managed in a Last-In-First-Out (LIFO) manner.

Example:

public void exampleMethod() {
    int x = 10; // Allocated on the stack
    String text = "Hello"; // Reference is on the stack, but the actual string object is in the heap
}

Heap Memory

The heap is a region of memory used for dynamic allocation of objects and class instances. When objects are created, their memory is allocated in the heap, and they remain in memory until they are no longer referenced and garbage collected.

Characteristics:

  • Memory Allocation: Happens when objects are created using the new keyword or literals (e.g., String literals).
  • Scope and Lifetime: Objects exist in the heap as long as they are referenced somewhere in the application. They are only deallocated when no longer reachable, at which point the garbage collector cleans up.
  • Storage: Stores actual object data (e.g., properties of an object).
  • Speed: Generally slower to access than stack memory, due to the dynamic nature and complexity of heap management.

Example:

public void exampleMethod() {
    String text = new String("Hello"); // The actual String object is stored in the heap
    int[] numbers = new int[5]; // The array is allocated on the heap
}

Key Differences and Timing

AspectStackHeap
Allocation TimeWhen methods are called (for local vars)When objects are created with new
DeallocationAutomatic, on method completionManaged by Garbage Collector
Data StoredLocal variables, references to objectsObjects, class instances
LifetimeMethod lifetime (short-term)Application lifetime or until GC (long-term)
Access SpeedFast (organized by LIFO)Slower (dynamic memory management)

Summary

  • Primitive types (e.g., intdouble) and references to objects declared within methods are stored in the stack.
  • Objects and class instances created dynamically are stored in the heap.
  • Stack memory is released automatically when a method completes, while heap memory is managed by the garbage collector and cleaned up when objects are no longer in use.

Post a Comment

Previous Post Next Post