3

My question is: When is an anonymous object swept by the garbage collector in Java?

The code is:

class Test extends Thread
{
    Test(){}
    public void run()
    {
        for(int i=0;i<4;i++)
            System.out.println(this.getName()+"i="+i);
    }
    protected void finalize()
    {
        System.out.println("Finalized");
    }
    public static void main(String args[])
    {
        new Test().start();
    }
}

From what I know about Java, any unreferenced object is swept by the GC. But there is no object reference here. Although the garbage collection process cannot be predetermined, but, still, when would the GC be "probably" done?

skrtbhtngr
  • 2,223
  • 23
  • 29
  • possible duplicate of [Java Finalize method call](http://stackoverflow.com/questions/2506488/java-finalize-method-call) – Jeffrey Apr 27 '14 at 13:48
  • In particular, [Joachim's answer](http://stackoverflow.com/a/2506509/758280). – Jeffrey Apr 27 '14 at 13:49
  • There is no such thing as an anonymous object. What you have is an instance of an anonymous class, and this instance is GCed exactly the same way as instances of non-anonymous classes. – JB Nizet Apr 27 '14 at 13:49
  • But, there are no references to the object; does that affect the GC precess? – skrtbhtngr Apr 27 '14 at 13:51
  • @Jeffrey: this question is a lot different from the one you specified. I am not asking about the invocation of finalize(). – skrtbhtngr Apr 27 '14 at 13:53
  • @skrtbhtngr Of course there is a reference to it, and it's even used: `this` inside `run()`. –  Apr 27 '14 at 13:53
  • Yes. it affects it. As soon as the thread has ended, the object is not reachable anymore, and it's thus eligible to GC. And sorry, but I read your code too fast. What you have is not an instance of an anonymous class as I told, but an instance of a top-level class. – JB Nizet Apr 27 '14 at 13:53
  • If you print `System.out.println(Thread.currentThread());` you will see it is the `Test` instance you created. – Peter Lawrey Apr 28 '14 at 07:29

4 Answers4

2

An object is eligible for garbage collection when there are no references to it and any live thread is not accessing it. When to garbage collect it depends on JVM

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • That's incorrect. If that were true, a.setB(b); b.setA(a); would lead to a memory leak. – JB Nizet Apr 27 '14 at 13:50
  • @JBNizet Ohh!!. Could you please enlight more sir? – Prasad Kharkar Apr 27 '14 at 13:51
  • 7
    The keyword is reachability. See http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29#Reachability_of_an_object and http://javarevisited.blogspot.fr/2011/04/garbage-collection-in-java.html. In short, an object is eligible for GC if it's not reachable from any root by following a chain of references. The roots are the static variables, and the stacks of the threads. So if a and b reference each other, but neither a nor b is reachable, then they're both eligible for GC. – JB Nizet Apr 27 '14 at 13:57
  • @PrasadKharkar JB Nizet is correct. The example he showed is called an "island of isolation". The JVM is smart enough to pick up on these things – Vince Apr 27 '14 at 15:43
1

Actually garbage collection will remove any objects that not reachable from the stack (= all active function calls in all threads).

When you call start() you are creating a new thread and java will call run() for you. In created this thread, the reference to your Test object is copied to the stack of the new thread. And by calling your run() is on the stack of the new thread.

When your run() function is done, the thread will be removed. And the Test object will be no longer referenced, and can be cleaned up. (in java you normally speak about eligible for cleanup, so you say: it is possible that it will be cleaned, but as always the garbage collection decides if/when it really happens.

Thirler
  • 20,239
  • 14
  • 63
  • 92
0

Actually garbage collector will pick up objects when they are not accessible by any live threads. So in your case object you created will be up for GC as soon as thread exits main method.

sasmit
  • 24
  • 3
  • 2
    That's incorrect. The started thread will continue to run unit its run method returns, even if the main method has ended. – JB Nizet Apr 27 '14 at 14:02
  • Object in question is created in method. So when ever method is over all of it's content is wiped out. – sasmit Apr 27 '14 at 14:06
0

Imho the answer is still: It cannot be said. However, in this case in can only be removed by the GC after the thread finished its work. The actual "finalization" time depends on some more factors.

nils
  • 1,362
  • 1
  • 8
  • 15