Quick revision
The short answer
The message after OutOfMemoryError tells us which memory ran out: Java heap space means the heap is full of live objects, GC overhead limit exceeded means the collector works constantly for little gain, Metaspace or Compressed class space means too many classes, Direct buffer memory means direct ByteBuffers exceeded their limit, unable to create native thread means the OS refused a new thread, and Requested array size exceeds VM limit means one array was too large. Each points to a different cause and a different tool.
What to remember
In this answer, we went through the OutOfMemoryError messages and what each points to. Java heap space and GC overhead limit exceeded mean the heap is full of live objects, from a leak or a heap that is too small. Metaspace and Compressed class space mean too many classes, usually a class loader leak. Direct buffer memory means direct ByteBuffers passed MaxDirectMemorySize. unable to create native thread means OS limits on threads or memory, and Requested array size exceeds VM limit means one impossible array. A heap dump helps most for the heap errors and also for metaspace and direct buffers.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The message after OutOfMemoryError tells us which memory ran out: Java heap space means the heap is full of live objects, GC overhead limit exceeded means the collector works constantly for little gain, Metaspace or Compressed class space means too many classes, Direct buffer memory means direct ByteBuffers exceeded their limit, unable to create native thread means the OS refused a new thread, and Requested array size exceeds VM limit means one array was too large. Each points to a different cause and a different tool.
Why this question is asked
OutOfMemoryError sounds like one problem, but the JVM manages several separate memory areas, and the error can come from any of them. Treating every OOM as a too-small heap leads to raising -Xmx for problems that have nothing to do with the heap. Interviewers ask this question to see whether we can read the message and go straight to the right area and the right tool. In this answer, we will recap the JVM's memory areas, go through each message with what causes it and how to confirm it, and finish with which of them a heap dump can help with.
1A Quick Recap of Where the JVM Keeps Memory
The Java heap holds the objects our code creates with new, and its size is limited by -Xmx or, in containers, by a percentage of the memory limit. Outside the heap, the JVM also uses native memory for several things: metaspace for class metadata, thread stacks for every platform thread, the code cache for JIT-compiled code, the garbage collector's own data structures, and direct buffers, which are ByteBuffers whose contents live in native memory so I/O can use them without copying.
Each area has its own limit and its own failure. So the first thing to read in an OutOfMemoryError is the text after the colon.
2Java heap space
This is the most common one. The JVM tried to allocate an object, ran a full garbage collection, and still did not find enough free space. That means the heap is full of objects that are still reachable, so the collector is not allowed to free them.
There are two possible reasons. Either there is a leak, objects that should have become unreachable but are still referenced, such as an ever-growing cache or a static list, or the heap is simply too small for the legitimate working set, for example a request that loads a 2 GB report into memory. Heap occupancy after GC separates the two: a steady upward trend suggests a leak, a sudden spike one large operation.
The stack trace shows where the allocation failed, which is not necessarily the code that filled the heap. It is just the unlucky allocation that came last. So the stack is a hint, and a heap dump is the evidence.
3GC overhead limit exceeded
This message comes from the Parallel collector. It means the JVM spent almost all of its time in garbage collection, by default more than 98 percent, while recovering very little memory, less than 2 percent of the heap, over several collections in a row. Rather than letting the application crawl forward forever, the JVM gives up with an error.
The cause is the same as for heap space, a live set close to the maximum heap, but it shows up earlier, while the application is thrashing. With G1, the default collector, the same situation usually shows up as very frequent collections and eventually Java heap space; whether G1 also implements this limit depends on the JDK version, so check yours.
4Metaspace and Compressed class space
Metaspace holds class metadata: the structures that describe each loaded class, its methods and its constant pool. It is native memory, unlimited by default, but often capped with -XX:MaxMetaspaceSize. Compressed class space is a separate fixed-size region inside it, 1 GB by default, used for the class structures themselves when compressed class pointers are enabled. Either can run out with its own message.
Running out usually means too many classes are being loaded and never unloaded. The classic cause is a class loader leak: an application is redeployed in a server, or a framework generates classes at runtime, and something still references the old class loader, so none of its classes can be unloaded. Generating new classes on every call has the same effect, and a steadily rising loaded-class count confirms it.
5Direct buffer memory
Direct ByteBuffers keep their contents in native memory. Their total size is limited by -XX:MaxDirectMemorySize, which defaults to the maximum heap size. When a new direct buffer would exceed that limit, even after the JVM tries a GC to free unreachable buffers, Java code in the NIO library throws this error. Recent JDKs phrase the message as Cannot reserve N bytes of direct buffer memory, with the current usage and limit; check your version.
The usual causes are libraries such as Netty or NIO-based HTTP clients holding many buffers, buffers allocated per request and kept too long, or a limit that is simply too low for the I/O load. The small Java objects that own the native memory, instances of DirectByteBuffer, live on the heap, which matters for the follow-up question below.
6Unable to create native thread and Requested array size exceeds VM limit
unable to create native thread means the JVM asked the operating system for a new thread and the OS refused. The usual reasons are a process or user thread limit (ulimit -u, or a container pids limit), or not enough native memory for another thread stack, which by default reserves about 1 MB on Linux for 64-bit JVMs. It almost always points to code that creates threads without bounds, such as a new thread or a new thread pool per request. A thread dump showing thousands of threads with similar names confirms it.
Requested array size exceeds VM limit means the code tried to create a single array larger than the JVM allows, which is slightly below Integer.MAX_VALUE elements. It is rarely a memory shortage at all; it is usually a bug that computes a huge size, or code that tries to grow a buffer or collection past its maximum, and the stack trace points right at it.
| Message | Memory area | Usual cause | First tool |
|---|---|---|---|
| Java heap space | heap | leak or heap too small | heap dump, post-GC trend |
| GC overhead limit exceeded | heap (Parallel GC) | live set near max heap | GC log, heap dump |
| Metaspace | native, class metadata | class loader leak | class count, heap dump |
| Compressed class space | native, class structures | too many classes | class count, heap dump |
| Direct buffer memory | native, direct buffers | too many or too big buffers | heap dump of DirectByteBuffer |
| unable to create native thread | OS threads | unbounded thread creation | thread dump, ulimit |
| Requested array size exceeds VM limit | one array | bug computing a huge size | stack trace |
7Follow-up questions
Which of these can a heap dump help with?
A heap dump shows objects on the heap and what references them, so it is the main tool for Java heap space and GC overhead limit exceeded. It also helps with Metaspace, because class loaders are heap objects and the dump shows which old loader is still referenced and by what. For Direct buffer memory it helps indirectly: the native memory is not in the dump, but the DirectByteBuffer objects that own it are, so we can see who holds them. It does not help with unable to create native thread, where a thread dump and OS limits are the evidence, and for the array size error the stack trace is usually enough. Note that -XX:+HeapDumpOnOutOfMemoryError only fires for errors the JVM raises itself, so a Direct buffer memory error thrown by library code does not trigger it.
Is it ever right to just raise -Xmx?
Yes, when the evidence shows legitimate growth: post-GC occupancy is flat at a level close to the maximum and the working set is explained by real data. But we raise it only after confirming it is not a leak, and in a container we keep room for native memory, or the pod will be killed by the kernel instead.
What other memory failures look different from these?
If native allocation inside the JVM fails, the JVM may crash with a fatal error log, hs_err_pid, instead of throwing OutOfMemoryError. In containers, the kernel OOM killer can also kill the process with SIGKILL, exit code 137, without any Java error at all.
8Conclusion
In this answer, we went through the OutOfMemoryError messages and what each points to. Java heap space and GC overhead limit exceeded mean the heap is full of live objects, from a leak or a heap that is too small. Metaspace and Compressed class space mean too many classes, usually a class loader leak. Direct buffer memory means direct ByteBuffers passed MaxDirectMemorySize. unable to create native thread means OS limits on threads or memory, and Requested array size exceeds VM limit means one impossible array. A heap dump helps most for the heap errors and also for metaspace and direct buffers.
Open with
I read the text after the colon, because each message points to a different memory area. Java heap space means a full GC could not free enough room, so the heap is full of live objects, either a leak, which shows as post-GC occupancy rising over time, or a heap that is too small for the real working set.
Then, if they want more
GC overhead limit exceeded comes from Parallel GC when more than about 98 percent of time goes to GC and less than 2 percent of the heap is recovered; it is the same problem seen earlier. Metaspace or Compressed class space means too many classes are loaded, typically a class loader leak after redeploys or runtime class generation.
Direct buffer memory means direct ByteBuffers exceeded MaxDirectMemorySize, often from Netty or NIO buffers held too long. unable to create native thread means the OS refused a thread, either a process or pids limit or no native memory for the stack, which usually points to unbounded thread creation.
And Requested array size exceeds VM limit is one array near Integer.MAX_VALUE elements, normally a size bug. A heap dump helps with the heap errors, with metaspace through the class loaders, and with direct buffers through the DirectByteBuffer objects, but not with native threads, where I would use a thread dump.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "List the different OutOfMemoryError messages a JVM can produce, and what each one points...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/0c72ce6cca/