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.
- Check If you application closes all the resources gracefully, may in finally block or something like that.
- Check about the objects you are creating. Ask your self whether you really need any object to be created again and again, Can It be a singleton, What is the scope of that object- whether it is globally accesible, what is the life of that object.
- Try to minimize the scope of the object, I mean if its really important then make the object globally accessible.
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.