简体   繁体   中英

How to see what objects have been garbage collected in Java?

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).

You can use the finalize method from Object . This method is called when the object is about to be GCed. From here, you can log the information you need.

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.

在此输入图像描述

Weak references or phantom references seem to be useful to log when the object actually gets removed. In this article the technology is explained.

Are you searching for a memory leak?

If you implement finalize() method (available in every Object ) 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.

There is a nice built-in tool in JDK for observing jvm in runtime. It is jvisualvm. There is aa good reference with screenshots: http://visualvm.java.net/description.html

Here is one of them : 在此输入图像描述

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM