Hello Nick
My question is, once an object is allocated a syncblock does it ever go
away before the object is collected?
Yes. The object SyncBlock is reclaimed in certain conditions even if the
object is still alive. There are several articles that illustrate the point
in detail:
http://www.bluebytesoftware.com/blog/2007/06/24/CLRMonitorsAndSyncBlocks.asp
x
<quote>
So now back to the question: when are sync blocks reclaimed?
When a GC is triggered, objects in the reachability traversal may have
their sync blocks reclaimed, even if the object in question is still alive,
and made available again in the system-wide pool of reusable sync blocks.
This reclamation can happen so long as the sync block isn't needed
permanently (as would be the case if COM interop information was stored
inside of it) and the sync block isn't marked precious. A sync block is
precious anytime there is a thread inside of the object's critical region,
when a thread is waiting to enter the critical region, or when at least one
thread has registered its event into the associated condition variable
list. Notice that orphaning monitors can thus lead to leaking events,
because they will remain precious, unless the monitor object itself becomes
unreachable. When a sync block is reclaimed in this fashion, certain
reusable state is kept, like the critical region event object, so that the
next monitor to use the sync block can reuse it.
</quote>
http://msdn.microsoft.com/en-us/magazine/cc188793.aspx
<quote>
When an object is constructed, the object's SyncBlockIndex is initialized
to a negative value to indicate that it doesn't refer to any SyncBlock at
all. Then, when a method is called to synchronize access to the object, the
CLR finds a free SyncBlock in its cache and sets the object's
SyncBlockIndex to refer to the SyncBlock. In other words, SyncBlocks are
associated with an object on the fly when the object needs the
synchronization fields. When no more threads are synchronizing access to
the object, the object's SyncBlockIndex is reset to a negative number, and
the SyncBlock is free to be associated with another object in the future.
</quote>
<quote>
By the way, the SyncBlock cache is able to create more SyncBlocks if
necessary so you shouldn't worry about the system running out of them if
many objects are being synchronized simultaneously.
</quote>
Therefore, if the SyncBlock does not contain COM interop information and it
is not marked as precious, GC can reclaim the SyncBlock. Besides, the
SyncBlock cache can grow if more SyncBlock is needed, so you don't need to
worry the SyncBlocks will hit the limit of the numbers of objects in your
cache.
For additional information on what a SyncBlock contains, you can see
http://msdn.microsoft.com/en-us/magazine/cc163791.aspx.
If you have any questions, please be free to let me know. Have a nice day!
Regards,
Jialiang Ge (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.