Hye
Does the GC clear memory of running threads with no reference to them ?
Consider the following code:

Thread t = new Thread()...
t.start();
t = null;
.
.
.

Suppose 't' runs forever, and the rest of the program runs forever - will the GC clean 't' from the memory (stop it in the middle of the running) ? It's running but it has no reference to it.

Please if you have link to an official page from SUN/Oracle that support the answer, write it here, because I searched and did not find an official answer to it.

Thanks

Recommended Answers

A thread will continue executing until its run() method terminates, at which point it dies and cannot be restarted. I think the GC issue is a red herring - although you may have freed all your explicit references to it, the JVM scheduler surely has one.
This http://www.janeg.ca/scjp/threads/state.html

Jump to Post

one rule GC doesn't works inmediatelly, you can test that (anyTimes) with Runtime.getRuntime()#someMethods

your topic says nothing about Thread(s), reference from snoorOracle http://download.oracle.com/javase/tutorial/essential/concurrency/index.html

remove nonStatic reference "objectName = new ObjectType();"

Jump to Post

All 7 Replies

A thread will continue executing until its run() method terminates, at which point it dies and cannot be restarted. I think the GC issue is a red herring - although you may have freed all your explicit references to it, the JVM scheduler surely has one.
This http://www.janeg.ca/scjp/threads/state.html useful summary isn't from Sun/Oracle, but it does reference them.

I can see your point that the thread remains alive, it make sense, but I did not find it written in SUN / Oracle official pages...

You'll find some strong clues in the JavaDoc for Thread and ThreadState, but I can't find anything from Sun/Oracle that documents this properly. It must be somewhere?

I don't know.
well it makes sense that the thread will not die in the middle as said above, I just wanted to be sure by an official documentation.
Thanks for the responses.

This is because each thread spawned is effectively a garbage collection root i.e. the central point at which the GC starts working. As far as an official source is concerned, the same is mentioned (but in a bit cryptic way) here (read the unreachable section).

Thanks !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.