Java中如何查看哪些对象被垃圾回收了?
请问,有什么方法可以获取已在 Java 中进行垃圾收集的对象(它们的变量或至少是类名)的历史记录吗?
仅添加这些参数(到 Oracle JVM)
-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
不会提供任何其他内存(以字节为单位)。对我的帮助非常有限。感谢您的所有回复。
注意:不幸的是,使用 add finilize() 方法的解决方法对我来说不是一个选择(我无权访问它)。
Please, is there any way how to get history of objects (their variable or at least class names) that have been garbage collected in Java?
Just adding these params (to Oracle JVM)
-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
doesn't provide anything else memory in bytes. It's very limited help to me. Thanks for all responses.
Note: Workaround with add finilize() method is not unfortunatelly an option for me (I don't have an access to it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
Object
中的finalize
方法。当对象即将被 GC 时,会调用此方法。从这里,您可以记录您需要的信息。You can use the
finalize
method fromObject
. This method is called when the object is about to be GCed. From here, you can log the information you need.免责声明:我的公司开发了我在这个答案中推荐的工具。
在 JProfiler 中,您可以转到“记录的对象”视图内存部分并将活动模式切换为垃圾收集对象(查看->更改活动模式->垃圾收集对象)。然后您将看到已 GC 的对象的统计信息。
Disclaimer: My company develops the tool that I recommend in this answer.
In JProfiler you can go to the "Recorded objects" view in the memory section and switch the liveness mode to garbage collected objects (View->Change Liveness Mode->Garbage Collected Objects). You will then see a statistics of objects that have been GCed.
您是否正在寻找内存泄漏?
如果您实现
finalize()
方法(在每个Object
中可用),它将在对象被垃圾收集之前被调用 - 并且您可以在其中运行任何代码。如果您正在寻找一个系统的解决方案(或者您无权访问您想要监视的类),我不知道有任何 JVM 选项允许这样做。然而,您可以记录正在加载和卸载(GCed)的类,但这不是您所要求的。
Are you searching for a memory leak?
If you implement
finalize()
method (available in everyObject
) it will get called just before the object is being garbage collected - and you can run any code inside it.If you are looking for a systematic solution (or you don't have access to classes you want to monitor), I am not aware of any JVM option that allows that. However you can log classes being loaded and unloaded (GCed), but this is not what you are asking.
弱引用 或 幻像引用 似乎对于记录对象实际被删除时很有用。在 本文对该技术进行了解释。
Weak references or phantom references seem to be useful to log when the object actually gets removed. In this article the technology is explained.
JDK中有一个很好的内置工具,用于观察运行时的jvm。它是jvisualvm。有一个很好的屏幕截图参考: http://visualvm.java.net/description.html
这是其中之一:
希望有所帮助。
There is a nice built-in tool in JDK for observing jvm in runtime. It is jvisualvm. There is a a good reference with screenshots: http://visualvm.java.net/description.html
Here is one of them :
Hope that helps.