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
JetBrains dotTrace allows you to invoke garbage collection when youre attached to a process and capturing a trace of it. So there is a way... http://www.jetbrains.com/profiler/
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:
Or to quote from the
PerfView.exe /?
output: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):Example output looks like:
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.
This tool allows you to force garbage collection.. It must be started before the process you want to work with - so its not quite what you were looking for.
http://www.yourkit.com/dotnet/download/index.jsp
http://www.yourkit.com/docs/index.jsp
The garbage collector runs inside a process. So if you want to run the garbage collector for this process you could try the GC.Collect method. You cannot force garbage collection for a given process from the outside.
Also note that forcing garbage collection (using the
GC.Collect
) method is considered as bad practice and should be avoided.There is no Microsoft tool and I have never heard of any 3rd party tool capable of doing this. Each process gets its own GC heaps, and therefore its own GC threads, so forcing a GC Collection on another process, AFAIK, is impossible.