如何以编程方式拦截GC以将信息打印到我的日志中 [英] How to programmatically intercept the GC to print information to my log

查看:153
本文介绍了如何以编程方式拦截GC以将信息打印到我的日志中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以使用-verbosegc将关于GC的信息打印到sysout,但我不想那么做。我希望截获每当GC触发并打印有关它的信息到我的自定义记录器,也许保存上一个GC时间戳在一个内部变量等。



这里有任何希望吗?解决方案

您无法在Java程序中挂接GC日志记录机制,因为当GC线程运行JVM时有效停止。这样做会导致僵局,所以它是我们不能做的一件好事。即使使用自定义启动器尝试通过某个句柄重新打开钩子并调用Java并挂钩也会陷入僵局。也许如果从自定义启动器中重新打开是可能的(我不知道这种方式或其他),缓冲数据并通过JNI从Java跳转到本地以检索缓冲区可能会有效。


I am aware that you can use the -verbosegc to print information about the GC to the sysout but I don't want that. I want to intercept whenever GC triggers and print information about it to my custom logger, perhaps save last GC timestamp on a internal variable, etc.

Any hope here?

解决方案

You can't hook the GC logging mechanism from within a Java program because when the GC thread(s) run the JVM is effectively stopped. Doing so would cause a deadlock so its a good thing we can't. Even using a custom launcher to try and hook via some handle reopen technique and calling up to Java would deadlock. Maybe if a reopen from within a custom launcher was possible (I don't know one way or the other) buffering the data and jumping from Java to native via JNI to retrieve the buffer might work.

这篇关于如何以编程方式拦截GC以将信息打印到我的日志中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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