GC calls in for MTA / STA thread

M

Mrinal Kamboj

Hi ,

Some time back i had posted question regarding usage of GC.collect and
GC.WaitForPendingfinalizers calls , for which i had got appropriate num.
of convincing answers .

Now there's another point of contention , which as the subject explains
making such calls from a STA / MTA thread .

As per the developer i was talking to , these calls are kind of
important for STA threads , since for them GC thread can't enter the
apartment for cleansing , however for an MTA thread apartment , this is
not the case as GC can enter the apartment , but frankly saying it's
sounds crazy to me , since as per this logic :

1. There's no way GC can clean the memory allocated to STA thread
objects , since even these calls leads to GC thread invocation and i
think there n number of STA applications in the real world , which is
the default .

2. Also , even it's an STA memory allocated will be ultimately on heap
and Geno -> Gen1 -> Gen2 for .net , so this whole concept of entering
/interfering with an apartment doesn't comes in .

3. One more thing if for a given code executed on a thread and finalizer
is implemented , then while GC carries out finalization process , does
it need to enter the thread apertment , as in that case again STA thread
will be dealing with same issue .

I think i am able to somewhat convey the issue , i am facing , in case u
need more explanation / details , let me know that .

Any pointers / help would be great .

thanks ,

Mrinal
 
N

Nicholas Paldino [.NET/C# MVP]

Mrinal,

So you are saying that if you have a thread that is an STA thread, you
can't GC the objects created on it (even though objects don't really have an
idea of what thread they are created on in the heap)? I think you need to
clarify this, since most .NET components are run in an STA thread (even
though it doesn't do much unless you are using COM interop).

For .NET components, this isn't an issue, since STA and MTA don't apply.
They are for COM interop.
 
M

Mrinal Kamboj

Nicholas ,

what i meant was even if you create an object on given worker thread ,
it invariably gets allocated memory on the heap , so getting into the
apartment logic doesn't make sense .

Also as i can understand from your answer , STA and MTA doesn't creates
a diff. at all till the point we don't have COM component in the picture
, then that means for a pure .net stuff , it is ok to have either , it
will have a zero difference , are u sure ?

What if , i am accessing underlying unmanaged code that is in c/c++ ,
but not COM .

thanks ,

Mrinal
 
W

Willy Denoyette [MVP]

Inline

Willy.

Mrinal Kamboj said:
Hi ,

Some time back i had posted question regarding usage of GC.collect and
GC.WaitForPendingfinalizers calls , for which i had got appropriate num.
of convincing answers .

Now there's another point of contention , which as the subject explains
making such calls from a STA / MTA thread .

As per the developer i was talking to , these calls are kind of important
for STA threads , since for them GC thread can't enter the apartment for
cleansing , however for an MTA thread apartment , this is not the case as
GC can enter the apartment , but frankly saying it's sounds crazy to me ,
since as per this logic :

There is no such thing as a GC thread, but I guess you are refering to the
Finalizer thread..
1. There's no way GC can clean the memory allocated to STA thread objects
, since even these calls leads to GC thread invocation and i think there n
number of STA applications in the real world , which is the default .
That's not true, the GC runs on the current thread, he doesn't care about
apartments and is perfectly able to do it's job.
Note also that the STA default is only for the main UI thread in a window
forms application, any other application should only select STA when
absolutely needed.

2. Also , even it's an STA memory allocated will be ultimately on heap and
Geno -> Gen1 -> Gen2 for .net , so this whole concept of entering
/interfering with an apartment doesn't comes in .
The GC kows nothing about apartments and there is no such thing as STA
memory...Not sure what is meant here...
3. One more thing if for a given code executed on a thread and finalizer
is implemented , then while GC carries out finalization process , does it
need to enter the thread apertment , as in that case again STA thread will
be dealing with same issue .
The finalizer thread is a dedicated thread that runs your finalizer code
(wrongly called a destructor i c#). As the finalizer thread runs in a MTA
thread, it has to obey one of the rules of COM when calling into a STA, that
is that calls from incompatible apartments must be marshaled. Marshaling
MTA/STA calls is done using windows messages that get posted to the STA
thread's message queue, that means that the STA thread needs to pump
messages. Failing to pump will prevent the finalizers to be run, and finaly
when the queue gets filled-up completely, in the finalizer thread to block.
Note that in general this won't happen on a UI thread (by default a STA
thread) as each UI runs a message pump, the problem is with non UI type
applications where one thread enters an STA, here you have to make sure that
this thread pumps the message queue.
 
M

Mrinal Kamboj

Willy ,

thanks for the reply , but i remember reading somewhere on msdn that GC
gets invoked on internal CLR thread , which i am considering different
than main application thread .

Also isn't finalizer also part of GC , juat that it has dedicated thread
to run .

Let me search the link , do you think it's wrong ?

thanks ,

Mrinal
 
N

Nicholas Paldino [.NET/C# MVP]

Mrinal,

It is possible that a GC takes place on a different thread. However, on
workstation installs of the CLR, it will occur on the application thread.
On server installs, where there is more than one processor on the machine,
the CLR will choose to perform a GC on another thread.

What are your concerns here? What are you trying to do?
 
W

Willy Denoyette [MVP]

Nicholas Paldino said:
Mrinal,

It is possible that a GC takes place on a different thread. However,
on workstation installs of the CLR, it will occur on the application
thread. On server installs, where there is more than one processor on the
machine, the CLR will choose to perform a GC on another thread.

Provided you have enabled the server flavor GC in you config file, else you
get the workstation GC by default.


Willy.
 
W

Willy Denoyette [MVP]

Mrinal Kamboj said:
Willy ,

thanks for the reply , but i remember reading somewhere on msdn that GC
gets invoked on internal CLR thread , which i am considering different
than main application thread .

Also isn't finalizer also part of GC , juat that it has dedicated thread
to run .

Let me search the link , do you think it's wrong ?

Check Maoni's BLOG for details on GC stuff.
See [1] for a remark on stalled finalizer in STA threads and [2] might help
you to use the GC efficiently.
[1] http://blogs.msdn.com/maoni/archive/2004/11/04/252697.aspx


[2] http://blogs.msdn.com/maoni/archive/2004/06/15/156626.aspx

Willy.
 

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