Home > Back-end >  trigger an event when GC start
trigger an event when GC start

Time:07-20

hi one small question in c#, is this possible to trigger an event whenever Garbage Collector Start working?

if yes, how?

imagine this code:

while(True){

    if (Gc.IsWorking())
    {
        // some code
    }
}

running on a separate thread and detect when gc is working. i know that when gc triggered the whole application will pause , but can we some how disable it for some part of code like using unsafe so that way we can have that thread working?

CodePudding user response:

There is GC.TryStartNoGCRegion that can pause the GC. But this is for very specialized scenarios where you need very low and predictable latency. I think the intent is to have a rotating set of live nodes, and do any GC when nodes are rotated offline.

It might also be possible to use native code. I would not expect the GC to know or care about native threads.

But neither are solutions that should be used in any common scenario. Optimizing your allocation patterns will likely be sufficient for 99% of use cases.

  • Related