17

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

Community
  • 1
  • 1
Kamarey
  • 10,832
  • 7
  • 57
  • 70
  • 5
    To collect garbage from where? – Will A Aug 29 '10 at 08:29
  • 3
    From some process. I want to force GC for some diagnostics. It's not part of my code. – Kamarey Aug 29 '10 at 08:33
  • Useless answer: inside PowerShell run `[gc]::Collect(0)` to run a gen 0 collection in the process. But why would one want to do this? – Richard Aug 29 '10 at 09:32
  • 2
    @Richard: I assumed that there may be some tool, that can call GC for some specific process (say by ID). Like toolname.exe /GC /pid:1234. Thanks to Darin Dimitrov, I know there is no a such tool. But it could be, so I asked this question to be sure. And I still think that this is legal question and don't understand why you downvote it? – Kamarey Aug 29 '10 at 10:44

4 Answers4

39

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.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
9

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Thanks, but as I asked in the question: "without writing code". I assume that there any tool, that I can call from command line for some specific process. So sorry, your answer is not relevant. – Kamarey Aug 29 '10 at 08:32
  • @Kamarey, there is no Microsoft tool (and I doubt any 3rd party tool exists). Each process gets its own GC heaps, and therefore its own GC threads, so forcing a GC Collection on another process, as far as I know, is impossible. – Darin Dimitrov Aug 29 '10 at 08:34
  • 8
    @Kamarey how is it not relevant stating no it can't be done and giving you the reason? – Rune FS Aug 29 '10 at 08:36
  • Ok, thanks. So can you replace your answer with your last comment, so it will answer my question? – Kamarey Aug 29 '10 at 08:39
  • @Rune FS: sorry, I don't get your english. Can you explain what the problem? – Kamarey Aug 29 '10 at 08:40
  • @Kamarey, updated answer to include my comment. – Darin Dimitrov Aug 29 '10 at 08:42
  • 4
    Concerning the "no Microsoft tool". Apparently it is possible "now" (well might have been since 2012 ;-) using "PerfView". See my answer for more details an caveats. – Christian.K Sep 25 '14 at 07:51
4

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/

ewassef
  • 286
  • 2
  • 13
  • I think this is now in dotMemory which is the memory related part of the former dotTrace. The current dotTrace is the performance related part. – user829755 Jul 08 '18 at 13:36
2

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

Mel
  • 33
  • 6