How to find out that a specific object is no longer used in the application?

How can I understand that an object (or class objects) is not used and is ready to collect GC. or how can I find out that the object has no link while the application is running. (before he gets GCed)

+4
source share
3 answers

I assume that you mean that object detection is no longer used at runtime, and not something that you can check statically.

The easiest way to get notified that an object should be GCed is to override the finalize () method. Note. You must be careful what you do in this method. For example, it is single-threaded and blocking will not result in cleaning objects.

Another approach is to use weak or soft links and control the ReferenceQueue. This is a monitoring method when an object has been detected as available for cleaning. See Source for WeakHashMap for an example.

Note: There is no easy way to detect an object that is no longer used without a GC, and if you do not have a GC for a long time, you may not know in the meantime.

+2
source

in Eclipse you can right-click your class and choose References > Project

0
source

I use the UCDetector (Waste Code Detector) Eclipse plugin. It will show you public classes, methods or fields that have no references, and make it easy to delete them.

0
source

All Articles