Will it be GC in this condition?

P

Peter Lin

Dear all,
I have a local object tManager in method foo, I create it there and not use
it in the thread that created it anymore.
Will it be GC if the life time of _tsacThreadManager thread is the same
as main thread? I am not sure because the tsacThreadManager thread is running
the tManager's run method all the time.
void foo()
{
tManager= new SacThreadManager();
tManager.SetupThreadManagement();
ThreadStart _st = new ThreadStart(tManager.run);
_tsacThreadManager = new Thread(_st);
_tsacThreadManager.Name="sacThreadManager";
_tsacThreadManager.Start();
}

Thanks..
 
J

Jon Skeet [C# MVP]

Peter Lin said:
I have a local object tManager in method foo, I create it there and not use
it in the thread that created it anymore.
Will it be GC if the life time of _tsacThreadManager thread is the same
as main thread? I am not sure because the tsacThreadManager thread is running
the tManager's run method all the time.
void foo()
{
tManager= new SacThreadManager();
tManager.SetupThreadManagement();
ThreadStart _st = new ThreadStart(tManager.run);
_tsacThreadManager = new Thread(_st);
_tsacThreadManager.Name="sacThreadManager";
_tsacThreadManager.Start();
}

The SacThreadManager can't be garbage collected until the new thread
has finished executing (or at least, until the JIT can tell that the
"this" reference within SacThreadManager.run will never be read again).
 
P

Peter Lin

Thanks,
Now, I am more clear for this ....
The SacThreadManager can't be garbage collected until the new thread
has finished executing (or at least, until the JIT can tell that the
"this" reference within SacThreadManager.run will never be read again).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top