Java
Memory Management
Programming
Computer Science
Java Memory Pool

How is the java memory pool divided?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Java is known for its robust memory management capabilities, leveraging a sophisticated memory model that aids in optimizing runtime performance and scalability. The Java memory model principally divides memory into several regions or pools - each with distinct responsibilities and characteristics. Understanding this division is crucial for developers aiming to optimize applications and manage resources proficiently.

1. Heap Memory

Heap memory is where Java runtime instantiates all class instances and arrays. The heap is divided into several sub-regions known as Generations, each of which is targeted at optimizing garbage collection performance by grouping objects by their age and likelihood of being "garbage."

  • Young Generation: This is the place where most new objects are allocated. The Young Generation is further split into three parts:
    • Eden Space - Most objects are initially allocated here.
    • Survivor Spaces (S0 and S1) - Objects from Eden space are moved to one of the Survivor spaces if they are still referenced after a minor garbage collection.
  • Old Generation (Tenured Gen): Objects that have survived multiple rounds of garbage collection in the Young Generation are promoted to the Old Generation. This area is generally larger and is garbage collected less often, during what is known as a "Major GC" or "Full GC".
  • Permanent Generation (PermGen) or Metaspace (from Java 8 onwards): This area stores metadata information about the classes in use by the Java application. Prior to Java 8, PermGen was the region used, which had a fixed size and would throw an OutOfMemoryError if the class metadata exceeded this space. From Java 8, PermGen was replaced by Metaspace, which grows dynamically.

2. Non-Heap Memory

This consists of memory used by Java that is not part of the heap:

  • Code Cache: This is where the Java Virtual Machine (JVM) stores compiled method code, allowing faster method invocation.
  • Stack Memory: Each thread running in Java has its own thread stack, used for storing the frame of each method called, and this includes local variables, partial results, and other information necessary for method invocation and execution.

3. Direct Memory (Optional)

In addition to the regular JVM memory regions, applications can also allocate memory directly from the underlying operating system. This is generally managed through the ByteBuffer class and is used in scenarios requiring large buffers, like high-performance I/O.

Optimization and Garbage Collection

Properly managing memory in Java is a sophisticated task that involves understanding the allocation and deallocation cycles and the roles of various memory areas. Garbage collection (GC) in Java automatically handles the freeing up of memory resources by deleting references to objects that are no longer needed. The efficiency of GC is pivotal to application performance and is directly influenced by how well memory pools are utilized.

Different garbage collectors have strategies optimized for different kinds of heaps and application requirements, such as G1, CMS, and the default garbage collector.

Summary Table

Memory AreaSub-regionsPurpose
HeapYoung Gen, Old Gen, PermGen/MetaspaceStore class instances and arrays
Non-HeapCode Cache, Stack MemoryMethod operations and thread-specific values
Direct Memory (Optional)N/AUsed for high-performance operations outside regular heap

Conclusion

Thorough knowledge of how Java manages memory allows developers to write more efficient and higher-performance applications. By tweaking JVM options and selecting appropriate garbage collectors, developers can significantly affect how memory is managed, impacting overall application behavior under different loads. Memory management in Java is a broad topic, understanding its intricacies can provide a significant advantage in designing systems that are both robust and efficient.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.