Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GC on external process

Is it possible to force GC on an external process? I mean without attaching to Visual Studio/windbg etc. I know it might be possible to do it with something like VS Immediate window.

EDITenter image description here It looks like this could be done via PerfView but I could find much info as if this does a full GC or what. Any ideas?

like image 898
crazy novice Avatar asked Mar 03 '15 21:03

crazy novice


2 Answers

Your options for forcing a GC from an external process are

1) Attach with the debugger API and call a GC.Collect() method using that API. I tried to do this with PerfView originally but it does not work if the process is blocked in native code (the debugger does not attach until it runs managed code again), which was unacceptable.

2) Inject a profiler (which since V4.5 you can do with attach) and call an API on that to do a GC. This does work (assuming another profiler is not already attached). This is in fact what PerfView does.

3) Use Event Tracing for Windows (ETW). Since V4.5 of the runtime, there is a keyword called GCHeapCollect, that if set will cause the runtime to do a full GC. You need to be Admin to enable ETW providers, so this only works if you 'controlling' process can be admin. If you wish to do this, using the TraceEvent Nuget package (see http://blogs.msdn.com/b/vancem/archive/2014/03/15/walk-through-getting-started-with-etw-traceevent-nuget-samples-package.aspx) Normally ETW sends request to all processes on the system, but you can make it process specific (on Win 8 or later) using the TraceEventProviderOptions.

Note that none of this is meant for casual use. You should be something like a debugger or a profiler to be doing such things. Forcing GCs in other processes without their participation is generally evil....

Vance Morrison .NET Performance Architect.

like image 68
Vance Morrison Avatar answered Nov 15 '22 11:11

Vance Morrison


No, neither Windows nor .NET exposes any kind of API you could use to force a GC on a external process without attaching a debugger to it.

You must attach a debugger and use that route to force a GC.

like image 44
Scott Chamberlain Avatar answered Nov 15 '22 12:11

Scott Chamberlain