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.
source share