Catching java.lang.OutOfMemoryError?

前端 未结 14 1658
Happy的楠姐
Happy的楠姐 2020-11-22 06:01

Documentation for java.lang.Error says:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application

14条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 07:01

    There are definitely scenarios where catching an OOME makes sense. IDEA catches them and pops up a dialog to let you change the startup memory settings (and then exits when you are done). An application server might catch and report them. The key to doing this is to do it at a high level on the dispatch so that you have a reasonable chance of having a bunch of resources freed up at the point where you are catching the exception.

    Besides the IDEA scenario above, in general the catching should be of Throwable, not just OOM specifically, and should be done in a context where at least the thread will be terminated shortly.

    Of course most times memory is starved and the situation is not recoverable, but there are ways that it makes sense.

提交回复
热议问题