20

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

4
  • 5
    To collect garbage from where? Commented Aug 29, 2010 at 8:29
  • 3
    From some process. I want to force GC for some diagnostics. It's not part of my code. Commented Aug 29, 2010 at 8: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? Commented Aug 29, 2010 at 9: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? Commented Aug 29, 2010 at 10:44

4 Answers 4

41

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Amazing! Thansk a lot
Trying to do this in ASP.NET is resulting in a "System.UnauthorizedAccessException: Access is denied." in ICLRProfiling.AttachProfiler
@DannyMeister make sure you have the appropriate permissions to do so (the user running perfview.exe vs. the user running IIS).
I think there is a way to supress the new console window when running PerfView directly. e.g. PerfView.exe /logFile=log.txt /AcceptEula ForceGC [ProcessName | Process ID] as showing here
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.

7 Comments

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, 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.
@Kamarey how is it not relevant stating no it can't be done and giving you the reason?
Ok, thanks. So can you replace your answer with your last comment, so it will answer my question?
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.
|
5

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/

1 Comment

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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.