如何提前了解OutOfMemory或StackOverflow错误 [英] How to know about OutOfMemory or StackOverflow errors ahead of time

查看:138
本文介绍了如何提前了解OutOfMemory或StackOverflow错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,有没有办法知道一个 StackOverflow 错误或 OutOfMemory 异常可能会很快发生?

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

如果能够以编程方式获取内存使用统计信息,那么 OutOfMemory 异常可能会更容易,如果提前知道在抛出 OutOfMemory 异常之前需要使用多少内存。但是这些值是否可以知道?

The OutOfMemory exception might be an easier one to catch, if one is capable of getting memory usage statistics programmatically, and if one knows ahead of time how much memory needs to be used before the OutOfMemory exception is thrown. But are those values knowable?

对于 StackOverflow 错误,有没有办法获取递归深度,以及如何有没有人知道递归深度的值会导致错误发生?

For the StackOverflow error, is there a way to get recursion depth, and how does one know what value for recursion depth would cause the error to occur?

通过提前了解这些错误是否会发生,我觉得我可以更优雅地恢复应用程序,而不是看它崩溃。

By knowing ahead of time whether these errors will happen, I feel I can recover the application more gracefully, instead of watching it crash.

推荐答案

预计内存不足错误



我很惊讶,我没有看到在其他帖子中提到的,但你可以使用 ManagementFactory 在Java 5/6中获取大量的内存使用信息。

Anticipating Out of Memory Errors

I'm surprised I didn't see this mentioned in the other posts, but you can use ManagementFactory in Java 5/6 to get at a lot of the memory usage information.

看看平台mbean服务器页面了解更多信息在Java中检测低内存条件。相信当内存使用达到一定的阈值时,您可以设置通知程序来调用代码。

Look at the platform mbean server page for more information on detecting low memory conditions in Java. I believe you can setup notifiers to call code when memory usage reaches a certain threshold.

这篇关于如何提前了解OutOfMemory或StackOverflow错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆