Java – how to understand OUTOFMEMORY or stackoverflow errors in advance

In Java, is there any way to know that stackoverflow errors or OUTOFMEMORY exceptions may occur soon?

If a can programmatically obtain memory usage statistics, and if you know in advance how much memory needs to be used before throwing OUTOFMEMORY exceptions, OUTOFMEMORY exceptions may be easier to catch But are these values known?

For stackoverflow errors, is there any way to obtain the recursion depth, and how to know which value of the recursion depth will cause the error?

By knowing in advance whether these errors will occur, I think I can recover the application more gracefully than watching it crash

Solution

Expected out of memory error

I'm surprised I haven't seen this in other posts, but you can use managementfactory in Java 5 / 6 to get a lot of memory usage information

For more information about detecting low memory conditions in Java, see platform MBean server page I believe you can set up notifiers to call code when memory usage reaches a certain threshold

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>