nobalG
nobalG

Reputation: 4620

Object References,Threads and Garbage collection

One of the few things I know about Garbage collector is that it picks up the object which have no references. Let MyClass is a class and I make an object of it by doing

MyClass object = new MyClass();

I can make it eligible for garbage collection even when my code is executing by doing

object = null;

But what happened to the objects which have no object references, like following statement.

new MyClass();

My doubt is really concerned to threads, I can create and execute a thread by following code.

 public static void main(String args[])
 {
     new Thread() {
            public void run() {
                try {
                    System.out.println("Does it work?");
                    Thread.sleep(1000);
                    System.out.println("Nope, it doesnt...again.");
                } catch(InterruptedException v) {
                    System.out.println(v);
                }
            }  
        }.start();
 }

But the thread is having no reference to it, like you know I can create thread by having a object reference but May be I don't want to.

Upvotes: 1

Views: 625

Answers (3)

Solomon Slow
Solomon Slow

Reputation: 27115

...object which have no references...

Let's just be clear about what that means. Discussions about GC often distinguish objects that are traceable from a GC root from objects that are not traceable.

"Traceable" means, that you follow a chain of object references starting from a gc root, and eventually reach the object in question. The gc roots basically are all of the static variables in the program, and all of the parameters and all of the local variables of every active method call in every thread.

"Active method call" means that a call has happened for which the corresponding return has not yet happened.

So, if some active method in some thread has a reference to a Foo instance, and the Foo instance has a reference to a Bar instance, and the Bar instance has a reference to a Baz instance; then the Baz instance is "traceable", and it won't be collected.

In every thread, there are method activations below the run() call on the thread's stack, and at least one of those method activations has a reference to the Thread instance that manages the thread. So a Thread object will never be GCd while the thread that it manages still is running.

Also, somewhere in the dark heart of the JVM, there's a static list of all of the Class instances, so none of them will ever be GCd either.

Upvotes: 2

Rahul Tripathi
Rahul Tripathi

Reputation: 172398

The JVM keeps a reference to all the running thread so if a thread is running it will not be garbage collected. Also if your object is referenced by a running thread it would not be garbage collected.

Upvotes: 1

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27476

GC looks at all live threads for stack traces so your Thread doesn't have a reference from the main thread, but it does from the currently executing one (the newly created thread).

GC looks for the reachable objects from so called GC roots, one of the roots are all threads.

Upvotes: 4

Related Questions