6

i have read that object becomes eligible for garbage collection in following cases.

  1. All references of that object explicitly set to null.
  2. Object is created inside a block and reference goes out scope once control exit that block.
  3. Parent object set to null, if an object holds reference of another object and when you set container object's reference null, child or contained object automatically becomes eligible for garbage collection.

But is there anyway to identify that object which is eligible for garbage collection is collected by garbage collector?

Santosh Joshi
  • 3,290
  • 5
  • 36
  • 49
Dhruv Kapatel
  • 873
  • 3
  • 14
  • 27
  • 1
    Why do you need to know that? Any specific reason? – kosa Dec 11 '13 at 19:33
  • 2
    As long as you're referencing the object (directly or indirectly), it won't get eligible for garbage collection. So, when it finally gets garbage collected, you don't have a reference to it anymore, which means there's no way for you to identify it -- you must forget about it completely, or it won't get garbage collected. – Guntram Blohm Dec 11 '13 at 19:37
  • @Nambari.. i am reading articles about garbage collector working and this question arise in my mind. No other reason. – Dhruv Kapatel Dec 11 '13 at 19:43
  • Like others have said: your only option here is the `finalize` method. However, I have to tell you: I wrote thousands and thousands of lines of Java code and I have NEVER found myself in a situation where I needed to use `finalize`. When talking to other programmers, it never comes up. Except for satisfying whatever curiosity you have about it, I strongly recommend AGAINST using this method. I don't really know why they don't deprecate it... – Radu Murzea Dec 11 '13 at 21:01
  • @RaduMurzea: There are some [good uses of the finalize method](http://stackoverflow.com/questions/4215100/good-uses-of-the-finalize-method), although they're pretty rare. – maaartinus Dec 12 '13 at 00:56

2 Answers2

4

You can implement the Object#finalize() method

public class Driver {
    public static void main(String[] args) throws Exception {
        garbage();

        System.gc();
        Thread.sleep(1000);
    }

    public static void garbage() {
        Driver collectMe = new Driver();
    }

    @Override
    protected void finalize() {
        System.out.println(Thread.currentThread().getName() + ": See ya, nerds!");
    }
}

which prints

Finalizer: See ya, nerds!

So you can intercept right before collection. The javadoc states

The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads;

but also

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

After an object is garbage collected, JVM calls its finalize method. Default implementation does nothing; you can override it to, for example, print farewell message, or to close some opened resource.

Note however, that there is no guarantee as to how soon it is called after being collected.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38