Once you know the root cause of the problem, you can elaborate solutions to fix it. The problem could be a cache that grows indefinitely, a list that keeps collecting business-specific data in memory, a huge request that tries to load almost all data from database in memory, etc. This will give you very strong hints and you will (hopefully) be able to find the root cause of the problem. Here is a screenshot of VisualVM with a heap dump loaded:
It allows you to navigate the heap and see which objects use the most memory and what references prevent the garbage collector from reclaiming the memory. The heap dump file has all information about the memory usage of the application. VisualVM is a program located in JDK_HOME/bin/jvisualvm.
Use VisualVM to read the heap dump file and diagnose the issue. Investigate the issue using the heap dump file When you reproduce the problem and the application throws an OOM, it will generate a heap dump file. Well, if you cannot reproduce the problem in dev, you may have to use the production environment. This will tell the JVM to produce a heap dump when a OOM occurs: Start the application with the VM argument -XX:+HeapDumpOnOutOfMemoryError. Follow those steps to find the root cause of the OOM: Step 1. The root cause can be very hard to find in development if the problem cannot be reproduced. To solve that issue, you first need to find the root cause of it. However, it suddenly throws a OOM after several days in production at customer site. For instance, the application might behave flawlessly during development and QA. At this point, the JVM will throw an OOM (OutOfMemoryError).Ī memory leak can be very latent.
The garbage collector cannot collect those objects and the application will eventually run out of memory. A memory leak happens when the application creates more and more objects and never releases them. In many cases, like in the case of a memory leak, that second option is the only good solution. Improve or fix the application to reduce memory usage