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).
Java Garbage collector tracks the live object and objects which are no more need are marked for garbage collection. It relieves developers to think of memory allocation/deallocation issues. When an object created in Java program is no longer reachable or used it is eligible for garbage collection.
The garbage collector uses a traversal of the object graph to find the objects that are reachable. Objects that are not reached in this traversal are deemed garbage, even if they are part of a cycle of references.
As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory.
The garbage collection implementation lives in the JVM. Each JVM can implement garbage collection however it pleases; the only requirement is that it meets the JVM specification. Although there are many JVMs, Oracle's HotSpot is by far the most common.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With