Reputation: 2892
How can i catch java.lang.OutOfMemoryError: Java heap space
?
I have server which the work some times and after that throw java.lang.OutOfMemoryError: Java heap space
. But I can not faind place in code where it occurs. In logs not iformation where it occurs.
We allocated 8 GB memory and error does not appear but server at some point in time it starts to occupy almost the entire memory.
What tools will help me find the error?
I tried Visual JVM but it not helped. I can not faind code where memory leak
I can not emulate the situation when the server crashes
Upvotes: 1
Views: 83
Reputation: 2757
You can analyze the heap dump with some tool like JProfiler or VisualVM (there are many other tools and just mentioning two options here) to identify what kind of objects consume the most memory. This will give you an idea on the number of instances, memory consumption of different objects in you application. With that you will be able to figure out where the memory leak occurs.
Some tools like JProfiler lets you connect to a running JVM instance and track method call trees, locations of the code where a certain object is created etc, which will, combined with above details extracted from the heap dump, give you even more clues on where the memory leak occurs.
Upvotes: 1
Reputation: 1377
java.lang.OutOfMemoryError: Java heap space
You must not catch it. The programmer is not required to do anything with these. In fact it is a bad idea to use a try-catch clause for Errors. Recovering from Error is not possible. The only solution to errors is to terminate the execution. Errors are related to environment in which application is running. Examples include OutOfMemoryError, StackOverflowError, etc.
Now if you are facing this error, It means its time to re-engineering your application.
To analyze the heap dump, you can excute the JVM with below parameters:
-XX:+HeapDumpOnOutOfMemoryError writes heap dump on OutOfMemoryError (recommended)
-XX:+HeapDumpOnCtrlBreak writes heap dump together with thread dump on CTRL+BREAK
There is a good article on it. You can see here. It may resolve your problem. You can also use jhat - Java Heap Analysis Tool, its comes in JDK bin directory.
Upvotes: 0