asela38
asela38

Reputation: 4654

Is created thread instances resides in the Heap or are any where else?

After instantiating a Thread and start it what will happened to the created instance of it. Will it have same behavior as other instance?

Thread a = new MyThread();
a.start();

a = null

where this created instance of Thread resides(in heap or doesn't it be in tenured space). will it be garbage collected?. If it is get garbage collected what happened to used instance properties?

Upvotes: 0

Views: 173

Answers (2)

Stephen C
Stephen C

Reputation: 719396

A thread will not be garbage collected while it is "live", irrespective of whether the Thread object can be accessed. This is a consequence of the JLS's definition of reachability.

For the record, a typical JVM allocates a thread's stack in memory that is outside of the heap(s). The Thread object and its children are regular heap objects. These may be garbage collected: the specifications are silent on this, AFAIK. Finally, part of a thread's state may reside in memory managed by the OS kernel.


when I view the JVM through the JProfiler I was unable to find the instance of the MyThread which I create.

  1. That doesn't prove that it has been garbage collected. All it proves is that JProfiler couldn't find it.

  2. If the thread has terminated (and you haven't kept a reference to the Thread object) then it / they will no longer be reachable, and JProfile won't be able to find it.

Upvotes: 1

L. Cornelius Dol
L. Cornelius Dol

Reputation: 64065

By very definition, the Thread object is reachable by it's own thread as long as that thread is alive - so clearly, no, the Thread object will not be GC'd at least until the thread which was started lives.

Upvotes: 0

Related Questions