检测内存不足错误 [英] Detecting out of memory errors

查看:159
本文介绍了检测内存不足错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的系统提供一种检测内存异常是否发生的方法。这项工作的目的是通过JMX公开这个标志,并相应地采取行动(例如通过在监控系统上配置相关的警报),否则这些错误就被忽视了几天。

I would like to provide my system with a way of detecting whether out of memory exception has occurred or not. The aim for this exercise is to expose this flag through JMX and act correspondingly (e.g. by configuring a relevant alert on the monitoring system), as otherwise these errors sit unnoticed for days.

这样做的初衷是设置一个未捕获的异常处理程序< a>对于每个线程,并检查引发的异常是否为 OutOfMemoryError 的实例,并设置相关标志。但是,由于以下原因,这种方法是不现实的:

Naive approach for this would be to set an uncaught exception handler for every thread and check whether the raised exception is instance of OutOfMemoryError and set a relevant flag. However, this approach isn't realistic for the following reasons:


  • 该异常可能发生在任何地方,包括第三方库。没有什么可以做,以防止他们捕捉 Throwable 并保持自己。

  • 库可以产生自己的线程,我有没有办法为这些线程执行未捕获的异常处理程序。

  • The exception can occur anywhere, including 3rd party libraries. There is nothing I can do to prevent them catching Throwable and keeping it for themselves.
  • Libraries can spawn their own threads and I have no way of enforcing uncaught exception handlers for these threads.

我看到的一种可能的方案是字节码操作(例如附加某种方面在 OutOfMemoryError 的顶部),但是我不知道这是否正确的方法,或者这是否可行。

One of possible scenarios I see is bytecode manipulation (e.g. attaching some sort of aspect on top of OutOfMemoryError), however I am not sure if that's right approach or whether this is doable in general.

我们已经启用了 -XX:+ HeapDumpOnOutOfMemoryError ,但是我并没有看到这是解决这个问题的方法,因为它是为别的东西而设计的 - 它不提供Java回调当这种情况发生时。

We have -XX:+HeapDumpOnOutOfMemoryError enabled, but I don't see this as a solution for this problem as it was designed for something else - and it provides no Java callback when this happens.

有没有人这样做?你会如何解决它或建议解决它?欢迎任何想法。

Has anyone done this? How would you solve it or suggest solving it? Any ideas are welcome.

推荐答案

您可以使用内存不足警告系统;这个 http://www.javaspecialists.eu/archive/Issue092.html 可以灵感。您可以配置在某个内存阈值(例如80%)被触发后调用的侦听器 - 您可以使用此调用开始采取纠正措施。

You could use an out of memory warning system; this http://www.javaspecialists.eu/archive/Issue092.html can be an inspiration. You could configure a listener which is invoked after a certain memory threshold ( say 80%) is breached - you can use this invocation to start taking corrective measures.

我们使用某些东西类似地,当组件的内存阈值达到80%并启动清理操作时,我们暂停组件的服务;只有当使用的内存低于另一个可配置值阈值时,组件才会返回。

We use something similar, where we suspend the component's service when the memory threshold of the component reaches 80% and start the clean up action; the component comes back only when the used memory comes below a another configurable value threshold.

这篇关于检测内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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