M
Martin Maat
I am puzzled.
I have this object that uses a thread. The thread is encapsulated by the
object, the object has Start and Stop methods to enable the client to start
or stop the thread.
I found that the object will not be garbage collected while the thread is
running. Fair enough, the documented explanation is that the GC compresses
objects on the heap and needs to update references, the references will be
invalid for a couple of moments which could be a problem if any thread is
still using them while the GC is at work. So I have to stop my object
explicitely before the application shuts down. In fact, if I don't, the
application will not shutdown at all, the CLR will probably be waiting for
me to stop the thread so it can do its final cleanup. Like I said, fair
enough.
But hey... This means that any applicaon that has some background thread
running (which is not uncommon) will _never_ be subject to garbage
collecting while it is running. This can't be right. It is a very common
design pattern to have a modest background thread running that periodically
does some useful things and then goes to sleep for a couple of milliseconds.
Does this prevent the GC to kick in at all on the application's resources?
It seems so, the finalizer of my object does not kick in while the thread is
running.
What is true here and how do I go about it? It would help if there were a
way for the thread to say "Hey, GC, I will enter a safe state now just to
allow you to do your stuff. When you're done, wake me up again, okay?" But I
haven't found a way to do that (bluntly calling GC.Collect() from the thread
does not make any difference). So I am probably missing something. What is
happening? Is there really no garbage collection for applications that do
not take down all their threads on a regular basis?
Martin.
I have this object that uses a thread. The thread is encapsulated by the
object, the object has Start and Stop methods to enable the client to start
or stop the thread.
I found that the object will not be garbage collected while the thread is
running. Fair enough, the documented explanation is that the GC compresses
objects on the heap and needs to update references, the references will be
invalid for a couple of moments which could be a problem if any thread is
still using them while the GC is at work. So I have to stop my object
explicitely before the application shuts down. In fact, if I don't, the
application will not shutdown at all, the CLR will probably be waiting for
me to stop the thread so it can do its final cleanup. Like I said, fair
enough.
But hey... This means that any applicaon that has some background thread
running (which is not uncommon) will _never_ be subject to garbage
collecting while it is running. This can't be right. It is a very common
design pattern to have a modest background thread running that periodically
does some useful things and then goes to sleep for a couple of milliseconds.
Does this prevent the GC to kick in at all on the application's resources?
It seems so, the finalizer of my object does not kick in while the thread is
running.
What is true here and how do I go about it? It would help if there were a
way for the thread to say "Hey, GC, I will enter a safe state now just to
allow you to do your stuff. When you're done, wake me up again, okay?" But I
haven't found a way to do that (bluntly calling GC.Collect() from the thread
does not make any difference). So I am probably missing something. What is
happening? Is there really no garbage collection for applications that do
not take down all their threads on a regular basis?
Martin.