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.
EDIT 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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With