Is it possible to run .NET garbage collector from command line, e.g. without writing code?
Edit:
When asked this question, I meant exactly what asked here for Java garbage collector:
How to request JVM garbage collection (not from code) when run from Windows command-line
So if there is a way to do this in JVM, see no reason it wouldn't exist in .NET
There is an option, although I have no idea if that is "production safe". That is, I don't know how high the risk is, that the target process crashes. But if used for troubleshooting and/or analysis it might come in handy.
You can use PerfView for the purpose:
PerfView.exe ForceGC [ProcessName | Process ID]
Or to quote from the PerfView.exe /?
output:
... Usage: PerfView ForceGC Process
Forces a GC on the specified process
Parameters: Process The process ID or Process Name (Exe without extension) of the process to force a GC. ...
The "problem" here is, that this will open a new console window and, after it is done, prompt you to close this window.
PerfView.exe will however dump a slew of executables to %APPDATA%\PerfView\_version_
which are packed inside the PerfView.exe executable as resources.
So, once you have run the PerfView.exe command, you can invoke the HeapDump.exe
tool manually (in my case on x64 box and with process ID 15396):
cd C:\Users\MyUserName\AppData\Roaming\PerfView\VER.2014-02-04.09.06.52.000\AMD64
HeapDump.exe /ForceGC 15396
Example output looks like:
Loading the ETWClrProfiler.
Turning on debug privilege.
Highest Runtime in process is version v4.0.30319
0,0s: Trying to attach a profiler.
0,1s: Done Attaching ETLClrProfiler ret = 0
Attached ETWClrProfiler.
0,1s: Enabling JScript Heap Provider
0,1s: Enabling EtwClrProfiler
0,1s: Enabling CLR GC events
0,1s: Requesting a JScript GC
0,1s: Requesting a DotNet GC
4,0s: .NET GC Starting at 0,15s.
4,0s: .NET GC stats, at 0,16s Survived 2221152.
6,0s: .NET GC complete at 0,17s.
6,0s: Triggered .NET GC, No JScript heap detected
6,1s: Requesting ETWClrProfiler unload.
6,1s: Shutting down ETW session
[ 6,1s: Done forcing GCs success=True]
Please note, that the above is AFAIK not official use of the tool and might stop to work with new releases. And, of course, PerfView can do much more than just forcing a GC (start here).
Internally, the above uses the ICorProfilerInfo::ForceGC profiling interface/method that comes with the CLR (source. Writing a "simpler" / "standalone" tool for that purpose is thus not completely out of the question. Non trivial task never the less.
Update: PerfView as such is now open source and the tool talked about above is part of it. In case you're curious.
Update: I rolled my own version using the above mentioned tech. Works for me, but might not be all encompassing.