Skip to content

Instantly share code, notes, and snippets.

@chrisnas
Created December 6, 2018 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisnas/5e734a9598eeb4bd404bd9ecffe20333 to your computer and use it in GitHub Desktop.
Save chrisnas/5e734a9598eeb4bd404bd9ecffe20333 to your computer and use it in GitHub Desktop.
sealed class GcFinalizersEventListener : EventListener
{
// from https://docs.microsoft.com/en-us/dotnet/framework/performance/garbage-collection-etw-events
private const int GC_KEYWORD = 0x0000001;
private const int TYPE_KEYWORD = 0x0080000;
private const int GCHEAPANDTYPENAMES_KEYWORD = 0x1000000;
protected override void OnEventSourceCreated(EventSource eventSource)
{
Console.WriteLine($"{eventSource.Guid} | {eventSource.Name}");
// look for .NET Garbage Collection events
if (eventSource.Name.Equals("Microsoft-Windows-DotNETRuntime"))
{
EnableEvents(
eventSource,
EventLevel.Verbose,
(EventKeywords) (GC_KEYWORD | GCHEAPANDTYPENAMES_KEYWORD | TYPE_KEYWORD)
);
}
}
// from https://blogs.msdn.microsoft.com/dotnet/2018/12/04/announcing-net-core-2-2/
// Called whenever an event is written.
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
...
}
}
class Program
{
static void Main(string[] args)
{
GcFinalizersEventListener listener = new GcFinalizersEventListener();
Console.WriteLine("\nPress ENTER to trigger a few finalizers...");
Console.ReadLine();
for (int i = 0; i < 4; i++)
{
Thread t = new Thread(()=> {});
}
GC.Collect(2, GCCollectionMode.Forced, true, true);
Console.WriteLine("\nPress ENTER to exit...");
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment