JVM自动终止时如何生成线程转储 [英] How to generate Thread dumps when JVM terminates automatically

查看:117
本文介绍了JVM自动终止时如何生成线程转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题场景:在sonic MF容器(jvm)中注意到这个问题。容器托管了一些负责数据库操作和消息转换的java服务。一旦启动,容器运行正常2-3周并由其终止自己没有任何例外。

Problem scenario : The problem is noticed in sonic MF container (The jvm).The container has hosted some java services responsible for db operations and message transformations.Once started, the container runs fine for 2-3 weeks and terminates by its own without throwing any exceptions.

经过大量研究,我们无法找出导致jvm(MF容器)关闭的原因或原因。

After much research, we are unable to find out why or what has triggered the jvm (MF Container) to shutdown.

当jvm自动关闭时,有没有办法让线程转储?我正在使用java 1.6。我应该遵循这个问题吗?

Is there a way by which I can get the thread dumps when the jvm goes down automatically ? I'm using java 1.6. Is there any other approach to this problem I should follow ?

提前致谢。

推荐答案

您可以尝试 java.lang.Runtime.addShutdownHook(),让钩子遍历所有线程并使用 Thread.getAllStackTraces()。但是,如果JVM被 Runtime.halt()关闭,则不会调用挂钩。更复杂的是使用仪器来挂钩对<$ c的调用$ c> Runtime.exit()和 Runtime.halt()(或 Shutdown.sequence(),请参阅编辑#2),这样您就可以确切地看到在调用任何内容时发生了什么。

You could try java.lang.Runtime.addShutdownHook(), and have the hook iterate over all threads and dump their stack traces with Thread.getAllStackTraces(). However, if the JVM was shutdown by Runtime.halt() then the hooks won't be called. More complicated would be to use instrumentation to hook into the calls to Runtime.exit() and Runtime.halt() (or Shutdown.sequence(), see edit #2), so you can see exactly what's happening at the time that either is called.

编辑:另一种方法是安装 SecurityManager 不强制执行任何安全性,但是每当调用 SecurityManager.checkExit()时都会转储线程列表,因为 halt( ) exit()调用安全管理器方法。这比使用检测要容易得多,除了记录线程正在做什么之外,你甚至可以决定抛出异常。

EDIT: Another way of doing it would be to install a SecurityManager which doesn't enforce any security, but which dumps the list of threads whenever SecurityManager.checkExit() is invoked, since both halt() and exit() call that security manager method. This would be a lot easier than using instrumentation, and you could even decide to throw an exception in addition to logging what the threads are doing.

编辑2 :运行JVM的系统可以告诉JVM终止,在这种情况下使用安全管理器将无法正常工作。也不会在 Runtime.exit() Runtime.halt()上使用检测,因为被调用的方法是 java.lang.Shutdown.exit()。如果JVM由于最后一个守护程序线程完成而关闭,则调用 Shutdown.shutdown()。但是关闭钩子在这两种情况下工作。因此,即使您还要使用安全管理器或工具,也应始终使用关闭挂钩。

EDIT 2: The system the JVM is running on can tell the JVM to terminate, in which case using a security manager won't work. Nor will using instrumentation on Runtime.exit() or Runtime.halt(), since the method that gets invoked is java.lang.Shutdown.exit(). And if the JVM is shutting down because the last daemon thread finished then Shutdown.shutdown() is invoked. But shutdown hooks will work in either of those situations. So you should always use the shutdown hooks, even if you're also going to use the security manager or instrumentation.

这篇关于JVM自动终止时如何生成线程转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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