Basic Reasons For Java Memory Errors
Those of us in the Java world will at some time or other encounter challenges relating to memory errors or warnings. Often the messages we get are misleading in ways, for instance an OutOfMemory error when the JVM heap has plenty of free space. So I thought I should list down some of the causes I have encountered along with some from a very informative Oracle article located here So here are the messages and various possible causes...
If Java Virtual Machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion.
If insufficient memory can be made available to create the initial Java Virtual Machine stack for a new thread, the Java Virtual Machine.
If a computation requires more heap than can be made available by the automatic storage management system, the Java Virtual Machine.
If memory in the method area cannot be made available to satisfy an allocation request.
When creating a class or interface, if the construction of the run-time constant pool requires more memory than can be made available in the method area of the Java Virtual Machine.
If there is no start size specified for the Permanent Generation (pre Java 8 JVM's).
When the Tenured (Old) generation is full as there is nowhere else to go.
Check the Xms - Xmx JVM arguments and make sure they are not too close to the system overall RAM.
If the computation in a thread requires a larger Java Virtual Machine stack than is permitted.
If the computation in a thread requires a larger native method stack than is permitted.
Here is one I have seen many times and it baffled me for a while.
Exception in thread "main" java.lang.OutOfMemoryError: requested 32756 bytes for ChunkPool::allocate. Out of swap space?
I eventually tracked this to poorly written non-Java (native objects) which were being called from Java via proxies etc. This takes some pretty focused effort to resolve and is one more reason that load-testing is imperative before releasing code to production.