When your application has used up the entire allocated heap (-Xmx) and still needs more, you run into the classic OutOfMemory error. There is simply no more memory available for your application.
What are the symptoms for Heap Exhaustion?
1. Application is not responsive or extremely slow
2. Java.lang.OutOfMemory error in the Application log file
3. Possible Heap Dump file if the Application Server that is running your application is configured to do so
What causes Heap Exhaustion?
1. Inadequate memory (-Xmx setting). This issue mostly shows up when you are initially deploying an application and you have no idea about the memory requirements.
2. Memory Leak in the application. This is a biggie. Basically your application is holding on to objects and not cleaning them up. More on this in an upcoming post.
3. Sudden spike in load. A DOS attack can jolt your application by sending hundreds of thousands of request throwing it to consume all the heap in no time.
How to prevent Heap Exhaustion?
1. Configure adequate ‘max heap’ (-Xmx setting). You may not know the memory requirement at first. But be diligent to perform few load tests to arrive at an optimal value
2. Ensure there is no memory leak in the code. Profiling tool helps here a lot.
3. Additional note: Never set the ‘max heap’ more than the physical memory available in the system. When heap spills in to disk swap, the performance of your application will tank.
While Heap exhaustion is obvious, there is a cunning memory issue that can be difficult to solve – Heap Fragmentation.
Coming up: Troubleshooting GC: Heap Fragmentation