Omega
Omega

Reputation: 1599

How I can get a signal when GC starts running?

I'm developing C# .NET script and I want to know if during a method executing the Garbage Collector was called or not like handling a signal ?

I want to be sure that the time delay was caused by the GC .

Upvotes: 3

Views: 425

Answers (1)

Luaan
Luaan

Reputation: 63772

Well, when the GC is running, all your threads are suspended. So you can't do anything in managed code while GC is running.

There's a lot of options to get some kind of notification, though. For example, you could have a thread that does

var status = GC.WaitForFullGCApproach();

Note that you also have to use GC.RegisterForFullGCNotification to make this work. And of course, it has its own performance cost etc. Tweaking anything with how GC works is generally a bad idea.

If you want to find if there was a collection while some code run, you can use GC.CollectionCount before and after, and see if it changed.

Upvotes: 3

Related Questions