What are the different kinds of OutOfMemoryError in Java, and what causes each one?
There are several kinds of out of memory error, and they mean different things. The message that comes with the error tells you which one you have.
Java heap space means that the heap is full and the collector cannot free any more. It is the most common one, and it almost always means that your program is holding on to too much data.
GC overhead limit exceeded means that the collector has spent nearly all of its time trying and freeing almost nothing. It is really a symptom of the same problem, because the heap is nearly full of things that are all still alive.
Metaspace means that there is no more room for the information about your classes. It happens when a program loads a very large number of classes, or when it loads them again and again without letting the old ones go.
Unable to create native thread means that the process cannot ask the operating system for another thread. The memory that the thread stacks need is outside the Java heap, so the heap can look empty while this error still happens.
Direct buffer memory means that the memory outside the heap that the program asked for directly has run out.
Requested array size exceeds VM limit means that somebody asked for an array that is larger than the virtual machine will ever allow, which usually means a mistake with a number that was meant to be a size.
The next question in this topic looks at how to work out which one you have and what to do about it.