从命令行运行垃圾回收器? [英] Run garbage collector from command line?

查看:117
本文介绍了从命令行运行垃圾回收器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以从命令行运行.NET垃圾收集器,例如。无需编写代码?

Is it possible to run .NET garbage collector from command line, e.g. without writing code?

编辑:

当问这个问题时,收集器:

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

因此,如果有一种方法在JVM中这样做,看到没有理由它不会存在于.NET

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.

您可以使用 PerfView 用于:

PerfView.exe ForceGC [ProcessName | Process ID]

或引用 PerfView.exe /?输出:

Or to quote from the PerfView.exe /? output:


...
用法:PerfView ForceGC Process

... Usage: PerfView ForceGC Process

强制指定进程上的GC

Forces a GC on the specified process

参数:
进程进程的进程ID或进程名force
a GC。
...

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会转储一些可执行文件到%APPDATA%\PerfView\_version_

PerfView.exe will however dump a slew of executables to %APPDATA%\PerfView\_version_ which are packed inside the PerfView.exe executable as resources.

因此,一旦你运行了PerfView.exe命令,你可以调用 HeapDump.exe 工具(在我的情况下在x64框上,进程ID 15396):

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

输出示例如下:

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]

请注意,以上是AFAIK不是正式使用的工具,可能停止使用新版本。当然,PerfView可以做的不仅仅是强制GC(开始

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

在内部,上述使用 ICorProfilerInfo :: ForceGC 剖析CLR附带的界面/方法()。编写更简单/

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.

更新:PerfView是这样的现在是开源,以及上述工具是它的一部分。如果你好奇。

Update: PerfView as such is now open source and the tool talked about above is part of it. In case you're curious.

这篇关于从命令行运行垃圾回收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆