How to know about OutOfMemory or StackOverflow errors ahead of time

前端 未结 11 1706
余生分开走
余生分开走 2021-02-15 17:10

In Java, is there a way to know that a StackOverflow error or OutOfMemory exception may happen soon?

The OutOfMemory exception m

11条回答
  •  温柔的废话
    2021-02-15 17:40

    I don't know anything about working this out at run time, or what you might be able to do to avoid it once you predict it is going to happen. Better to try and avoid it occurring in the first place.

    1) You could use Findbugs which may indicate some StackOverFlow errors occurring from inadvertently calling the same method from itself.

    2) You could store data likely to cause you to run out of memory with a SoftReference and have a null check on accessing it so it can be reloaded if it's been garbage collected.

    If either of these things are actually issues for you then the solution probably isn't in detecting it happening but architecting your solution differently to avoid them occuring if at all possible.

提交回复
热议问题