The Java runtime manages the memory for you, but not magically. The objects live on the heap, the garbage collector reclaims the heap memory that is no longer reachable, and the stack holds the frames of the running methods. Understanding how those areas are divided explains most of what the people call the Java memory problems, which are usually either a leak in your own code or a heap that is simply too small for what you asked it to hold.
The previous lesson in this module covered "When Are Executors and Concurrent Collections the Right Tools?", and this lesson is the last one in the Java Multithreading, Files and the JVM module.
Where does the memory live?
There are three areas that you will meet early. The heap holds every object and every array, and it is where the garbage collector works. The stack holds the local variables and the bookkeeping for each method call, and it grows and shrinks as the methods are entered and left. The metaspace holds the class metadata, which is the description of your types themselves, and it lives outside the heap.
public class MemoryAreas {
static int staticField = 7;
int instanceField = 9;
public static void main(String[] args) {
int local = 5;
System.out.println("static field : " + staticField);
System.out.println("instance field : " + new MemoryAreas().instanceField);
System.out.println("local : " + local);
Runtime runtime = Runtime.getRuntime();
long total = runtime.totalMemory();
long free = runtime.freeMemory();
System.out.println("heap in use (MB) : " + (total - free) / (1024 * 1024));
System.out.println("max heap (MB) : " + runtime.maxMemory() / (1024 * 1024));
System.out.println("available processors : " + runtime.availableProcessors());
}
}The output is this.
static field : 7
instance field : 9
local : 5
heap in use (MB) : 1
max heap (MB) : 2548
available processors : 4Two distinctions explain most of the Java's behaviour. A local variable that is declared inside a method lives on the stack and it disappears when the method returns. A field of an object lives in the heap and it disappears when the object becomes unreachable. And a static field lives in the heap for as long as the class is loaded, which is why the static collections are the classic source of the leaks.
What does "unreachable" mean?
The garbage collector does not look for the objects that are "no longer used". It looks for the objects that cannot be reached from a root. The roots are the references that are held by the live thread stacks, by the static fields, and by the runtime itself. If nothing can reach an object, then it is garbage, even if some other object still holds a reference to it, as long as that holder is itself unreachable.
import java.util.ArrayList;
import java.util.List;
public class LeakDemo {
static final List<String> KEPT = new ArrayList<>();
static class Node {
final String name;
Node(String name) {
this.name = name;
}
}
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 1000; i++) {
Node node = new Node("node-" + i);
KEPT.add(node.name);
}
System.out.println("static list size : " + KEPT.size());
Runtime runtime = Runtime.getRuntime();
long used = runtime.totalMemory() - runtime.freeMemory();
System.out.println("heap in use (MB) : " + used / (1024 * 1024));
KEPT.clear();
System.gc();
Thread.sleep(100);
long after = runtime.totalMemory() - runtime.freeMemory();
System.out.println("after clearing (MB) : " + after / (1024 * 1024));
}
}The output is this.
static list size : 1000
heap in use (MB) : 1
after clearing (MB) : 1That is the whole model, and it explains how the leaks happen. A static collection that keeps growing is a root that keeps everything in it alive for ever, even though your program has no interest in the old entries. The program in the output holds a thousand names in a static list. Clear it and the memory becomes collectable again.
The same shape appears in the real applications. There is a cache with no eviction policy, there is a listener registry that never removes the listeners, and there is a static map that is used as a scratch pad.
What are the weak, the soft and the phantom references?
When you genuinely want an object to be collectable even though something still points at it, you can hold it through a reference wrapper. A WeakReference does not prevent the collection. A SoftReference is kept as long as the memory is comfortable, and it is dropped when it is not. A PhantomReference is for the cleanup callbacks, and it is rarely needed.
import java.lang.ref.WeakReference;
public class ObjectLifetime {
static WeakReference<String> tracked;
static void create() {
String local = "a string that can be collected";
tracked = new WeakReference<>(local);
System.out.println("inside create : " + tracked.get());
}
public static void main(String[] args) throws Exception {
create();
System.gc();
Thread.sleep(100);
System.out.println("after gc, still referenced : " + (tracked.get() != null));
}
}The output is this.
inside create : a string that can be collected
after gc, still referenced : trueThe output shows that the reference is still resolving immediately after the method returned. The garbage collector is allowed to run at any time, and asking it to with the call System.gc() is a hint and not a command. The important part is the contract. A weak reference never keeps its referent alive, so a cache that is built on the weak references cannot leak.
What happens when the memory runs out?
When the heap cannot satisfy an allocation, the collector runs harder. If it still cannot free enough, then the runtime throws an OutOfMemoryError. That is an Error and not an Exception, so a catch block for Exception will not catch it.
import java.util.ArrayList;
import java.util.List;
public class FinalizationAndErrors {
public static void main(String[] args) {
List<byte[]> blocks = new ArrayList<>();
try {
for (int i = 0; i < 200; i++) {
blocks.add(new byte[1024 * 1024]);
}
System.out.println("allocated 200 MB");
} catch (OutOfMemoryError e) {
System.out.println("caught : " + e.getClass().getSimpleName());
} finally {
blocks.clear();
}
System.out.println("program survived and cleaned up");
}
}The output is this.
allocated 200 MB
program survived and cleaned upThat program tries to allocate the two hundred megabytes at once. On a machine with a small default heap it fails, and the catch block catches the error so that the program can still exit cleanly. The lesson is not that you should catch it. You should not run out of the memory in the first place. But a long-running service should at least fail loudly rather than corrupt the state and carry on.
How does the collector decide what to free?
The modern collectors work on the observation that most of the objects die young, so they separate the objects by the age. The new objects are allocated in a small area and they survive a collection only if they are still reachable. The older objects that have survived several collections are promoted to a long-lived area, which is collected far less often.
The tuning knobs, which are the heap size, the region sizes and the pause targets, exist to balance the memory use against the time that the collector pauses the application. The practical advice is unchanged whatever the collector is. You do not create the millions of short-lived objects if you can avoid it, and you do not hold on to the objects that you no longer need.
How do you diagnose a memory problem?
- Read
Runtime.getRuntime()for the total, the free and the maximum heap figures. - Run with a small and explicit heap to make a leak show up faster:
-Xmx64m. - Take a heap dump when the problem appears and analyse which objects dominate it.
- Look for the static collections, the unbounded caches, and the listeners that are never removed.
- Remember that a WeakHashMap is the usual fix for a cache that is keyed by the objects that you do not own.
The habits to keep
- Keep the static collections small, or do not use them at all.
- Give the caches an eviction policy, or use the weak references.
- Let the collector do its job, and do not call
System.gc()in the production code. - Size the heap explicitly for the services whose working set you know.
- Remember that the OutOfMemoryError is an Error, so the ordinary exception handling will not catch it.
- Profile before you tune. The default settings are reasonable for most of the applications.
Try it yourself: write a program that keeps a static list of the objects in a loop, and that prints the heap usage every thousand iterations. Watch the used heap climb. Then replace the static list with a local list and watch the heap stay flat. Finally, add a WeakHashMap-based cache and show that the entries disappear once nothing else refers to their keys.