The error that occurs with Java.lang.OutOfMemoryError:Java heap space is either a program problem, or the memory allocated to the JVM is really not enough.
In general, can be controlled in advance to solve.
However, if an uncontrolled situation, such as using a third-party package, or a system cramp, throws a outofmemoryerror error. OH No, according to not throw, the current thread hangs straight away.
Now that they're all hung up, what are we talking about recovering? And hanging off is not just a matter of outofmemoryerror.
Under normal circumstances, outofmemoryerror in the case of uncontrolled, really really really do not need to deal with, simply ignore the good, when it is good luck. and to solve it, the logic of the program is difficult to see.
If you want to be serious, you want to die a little better when you are outofmemoryerror, or you can recover gracefully, then continue.
First understand some features of OutOfMemoryError:
1, uncertainty is not controllable. I believe you have solved the controllable situation.
2, no exception is thrown, which means that Try...catch does not work.
3, the current thread will hang when it appears.
4, on 3, if outofmemoryerror occurs in try...finally, then the finally statement is executed and then hung out. But do not think that try...finally is omnipotent, any situation can be implemented, you use Exit or shut down the computer to try.
5, to 3 points, if it is a sub-thread in multi-threading (non-guardian), hanging out does not affect other threads, and memory quickly (is soon, is soon) can be recycled.
So when you are sure you will encounter outofmemoryerror but can not solve the time, try...finally is useful, as long as no longer do a lot of memory operations, want to continue what process is no problem (I have not encountered a problem in the case). Exit is inevitable, finally the need to clean up, record status and so on. Also remember to use irrelevant independent threads to handle, and finally through the daemon, monitoring program or timer restart a processing thread just.
These days with Tika extract document content and do index, in the extraction of a 40M (other hundred m above no problem) of the document appears outofmemoryerror, memory is exhausted, to increase the upper limit of the system is useless, so it is finished, so execution will stop. The document is constantly increasing, and to extract content to do indexing, in order to make the program can be carried out smoothly and continuously, so the above exploration.
recovering from [Java.lang.OutOfMemoryError:Java heap space]