Back to Study Material Java

Java Memory Management

Java manages memory automatically, which is one of the language's most significant features for developer productivity. Unlike C or C++, where programmers ...

Java Memory Management

How Java Manages Memory

Java manages memory automatically, which is one of the language's most significant features for developer productivity. Unlike C or C++, where programmers must explicitly allocate and free memory, Java handles memory allocation and reclamation through a combination of the heap, the stack, and the garbage collector. Understanding how these components interact helps you write code that avoids memory leaks and performs well under load.

The JVM divides memory into several regions. The heap is where all objects created with new are stored. It is shared among all threads and is managed by the garbage collector. The stack, one per thread, holds local variables, method parameters, and return addresses. Stack memory is automatically reclaimed when a method returns; it does not require garbage collection.

Within the heap, the JVM historically used a generational structure based on the observation that most objects die young. New objects are allocated in the Eden space, part of the young generation. Minor garbage collections scan the young generation frequently and are fast because they process a small fraction of the total heap. Objects that survive several collections are promoted to the old generation, which is collected less frequently in more expensive major or full garbage collections.

The method area (called the Metaspace since Java 8) stores class metadata, static variables, and the bytecode of compiled methods. Unlike the heap, Metaspace grows dynamically and is not limited by the heap size, though you can cap it with JVM flags to prevent unbounded growth.

String literals and other interned strings are stored in the string pool, part of the heap since Java 7. The intern() method allows you to deduplicate strings manually, which saves memory in applications that create many equal strings from different sources.

Example showing object allocation:

String s = new String("hello"); // allocates on heap int x = 42; // stored on stack if local variable

Interview tip: Explain the difference between a memory leak in Java and a memory leak in C. In Java, a leak occurs when objects are still reachable through references even though they are logically no longer needed. The garbage collector cannot reclaim reachable objects, so removing references when objects are no longer needed is the developer's responsibility.

Stack vs Heap Memory

Distinguishing between stack and heap memory is fundamental to understanding Java's execution model. Every time a method is called, the JVM pushes a stack frame onto the calling thread's stack. The frame holds the method's local variables, parameters, and a reference back to the caller. When the method returns, the frame is popped, and all local data in it is immediately reclaimed. Stack allocation and deallocation are therefore extremely fast operations.

Primitive values (int, double, boolean, etc.) declared as local variables live entirely on the stack. Object references declared as local variables also live on the stack, but the objects they point to live on the heap. This distinction is important: the reference is on the stack, but the data it refers to is on the heap.

The stack is limited in size and is configured per thread using the -Xss JVM flag. Deeply recursive algorithms that create many stack frames without returning can exceed this limit, causing StackOverflowError. This is different from OutOfMemoryError, which indicates that the heap has no space for a new object allocation.

Objects created with new always go to the heap. The JVM's escape analysis optimization can sometimes allocate short-lived objects on the stack if it can prove the object does not outlive the method, but this is an internal optimization that you cannot control directly.

Heap memory is shared among all threads, which is why concurrent modifications to heap objects require synchronization. Stack memory is private to its thread; no synchronization is needed for local variables.

Example illustrating the difference:

public int compute() { int x = 10; // x lives on the stack StringBuilder sb = new StringBuilder(); // reference on stack, object on heap sb.append(x); return sb.length(); // sb is eligible for GC after this method returns }

After compute() returns, the stack frame is gone and the StringBuilder object on the heap becomes unreachable, making it eligible for garbage collection.

Interview tip: When asked whether primitives are always on the stack, clarify that it depends on context. Primitive instance fields of an object live on the heap as part of the object. Only local variables and method parameters declared as primitives reside on the stack.

Memory Tuning and JVM Flags

The JVM provides numerous command-line flags that let you control memory behavior. Proper tuning reduces garbage collection pauses, prevents OutOfMemoryError, and can significantly improve application throughput. Knowing the key flags and what they control is an important skill for Java developers working on performance-sensitive applications.

The most fundamental flags are -Xms and -Xmx, which set the initial and maximum heap size. Setting them to the same value prevents the JVM from resizing the heap during runtime, which can improve startup predictability and reduce GC overhead:

java -Xms512m -Xmx2g -jar myapp.jar

The young generation size is controlled by -Xmn or the ratio flag -XX:NewRatio. A larger young generation means less promotion to the old generation and fewer expensive major GCs, but reduces the space available for long-lived objects. Tuning this ratio depends on your application's object lifetime profile.

Since Java 8, the PermGen space was replaced by Metaspace, which is limited by -XX:MaxMetaspaceSize. If your application dynamically loads many classes (common in application servers), setting this flag prevents Metaspace from consuming all available native memory.

Enabling GC logging helps you understand what the garbage collector is doing in production. Use -Xlog:gc* (Java 9+) or -verbose:gc (older versions) to print GC events to the console or a file. These logs show collection frequency, duration, and how much memory is reclaimed, which are essential inputs for tuning decisions.

String deduplication is an option available with G1 GC that automatically removes duplicate String objects from the heap: -XX:+UseStringDeduplication. This can reduce heap usage by 10-25% in applications that create many strings from parsing data.

Example heap usage monitoring at runtime:

Runtime rt = Runtime.getRuntime(); long usedMemory = rt.totalMemory() - rt.freeMemory(); System.out.println("Used heap: " + usedMemory / 1024 / 1024 + " MB");

Interview tip: Explain the danger of setting -Xmx too high. If the heap is larger than available physical RAM, the OS will swap heap pages to disk during GC, causing stop-the-world pauses that last seconds or minutes instead of milliseconds. Always leave a buffer for OS and non-heap memory.