Maanu
Maanu

Reputation: 5203

Native thread behavior at the time of garbage collection

When the runtime starts garbage collection, it suspends all managed threads.. My application has both managed and unmanaged componenets. There is a possibility that a native thread from the unmanaged code can enter into the managed code when runtime executes the garbage collection activity.

How does .net handles this? Will .net suspends the native thread or something else happens?

Upvotes: 3

Views: 329

Answers (1)

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

As I understand it, the reason the managed threads are suspended, is that the managed heap may be compacted during collection. If this happens references to managed objects must be adjusted. I take it that your native threads are not using objects on the managed heap and thus should not be affected by garbage collection.

If your native threads do access managed objects, you should pin these. This will prevent the GC from moving the objects during compaction.

Upvotes: 4

Related Questions