querying the heap is very, very expensive. It means examining every object in the heap to see what is being referenced.
What you can do in Java is hold a WeakReference and after a garbage collection this will disappear. If it is retained there is still a use somewhere.