Memory Heap

What is Memory Heap?

The heap is a special area in a computer’s memory used by programs to store data while the program is running. When a program creates something new (like a text message, image, or user info), it goes into the heap. This memory is managed automatically, meaning the program adds or removes items from it as needed.

Unlike a stack, where items are removed in the order they were added (first-in, last-out), the heap has no fixed order. Data in the heap can live for a short or long time, depending on how the program uses it. If a piece of data is no longer needed, the memory is reclaimed and can be reused.

Technically, a heap is a portion of memory used for dynamic allocation. The blocks of memory in the heap are frequently allocated and freed based on the status of the object. The allocation and release of memory take place in an arbitrary order, and hence, unreferenced or dead objects exist in the heap. The Java program’s Garbage Collector reclaims the memory of the unreferenced objects.

Memory Heap Generation Structure
Figure 01: Memory Heap

How is the Heap Organized?

To make memory management more efficient, the heap is divided into different parts called generations. This setup helps the system handle short-lived and long-lived data differently, which saves time and improves performance.

1. Young Generation (Young Gen)

  • This is the area where all new objects are created.
  • It’s divided into three parts:
    • Eden Space: Where objects are first placed.
    • Survivor Space 1 (S1) and Survivor Space 2 (S2): Temporary holding areas for objects that survive one or more rounds of memory cleaning.
  • Most objects are small and short-lived. If they’re not needed quickly, they are removed.
  • Objects that survive a few rounds are moved to the Old Generation.

2. Old Generation (Tenured Gen)

  • This is where long-lived objects go.
  • Once an object has been used for a while and survived several cleanups in the Young Generation, it is moved here.
  • This space is larger than the Young Generation and is cleaned less often.
  • Example: A shopping cart in an online store app may stay in memory while you shop, so it lives longer and may move to the Old Gen.

3. Permanent Generation (PermGen) / Metaspace

  • This area stores metadata—information about the program itself, such as class definitions and method names.
  • In older Java versions, this was called PermGen.
  • In newer Java versions (Java 8 and above), it has been replaced by Metaspace, which automatically grows in size as needed and stores class-level data.
  • Developers generally don’t need to manage this space directly.

Why does this structure matter?

Splitting the heap into generations helps the program:

  • Quickly clean up short-lived objects (which are common).
  • Avoid wasting time checking old objects every time.
  • Use memory in a smart and efficient way.
  • Keep programs running smoothly, even as they create and remove lots of objects in real-time.

What happens when an Object is no longer needed?

When the program no longer needs an object (like when you’re done with a file or an image), Java automatically removes it from memory using a process called Garbage Collection (GC).

This is like cleaning up your desk and removing stuff you’re done with, so there’s space for new things.

Real-Life Example

Let’s say you are using a food delivery app:

  • When you open the app and start browsing, many short-lived objects are created (like menus, images). These go into the Young Generation.
  • If you keep your order in the cart, the cart object stays longer and may move to the Old Generation.
  • Information about how the app is built (like how classes and methods are defined) is stored in Metaspace.

How to monitor Heap Memory?

Even though memory is managed automatically, developers can monitor and tune it if needed:

  • Tools like VisualVM, JConsole, or Java Mission Control help track how much memory is used.
  • You can even set heap size limits using Java commands like:
    • java -Xms256m -Xmx1024m <MyApp>
      • -Xms: Starting heap size
      • -Xmx: Maximum heap size

This is useful when running large applications where memory efficiency matters.

Summary

  • The heap is where Java stores objects created at runtime.
  • It is divided into Young Generation, Old Generation, and Metaspace.
  • Garbage Collection removes objects that are no longer used.
  • Developers can monitor and tune heap usage for better performance.
  • The heap plays a key role in how Java keeps apps running smoothly

You may be interested: