.net - 从命令行运行垃圾收集器?

标签 .net command-line garbage-collection

是否可以从命令行运行 .NET 垃圾收集器,例如不写代码?

编辑:

当被问到这个问题时,我的意思正是这里对 Java 垃圾收集器的要求:

How to request JVM garbage collection (not from code) when run from Windows command-line

因此,如果有一种方法可以在 JVM 中执行此操作,那么它没有理由不存在于 .NET 中

最佳答案

有一个选项,尽管我不知道这是否是“生产安全”。也就是说,我不知道目标进程崩溃的风险有多高。但如果用于故障排除和/或分析,它可能会派上用场。

您可以使用PerfView目的:

PerfView.exe ForceGC [ProcessName | Process ID]

或者引用 PerfView.exe/? 输出:

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

这里的“问题”是,这将打开一个新的控制台窗口,并在完成后提示您关闭此窗口。

PerfView.exe 但是会将大量可执行文件转储到 %APPDATA%\PerfView\_version_,这些可执行文件作为资源打包在 PerfView.exe 可执行文件中。

因此,运行 PerfView.exe 命令后,您可以手动调用 HeapDump.exe 工具(在我的例子中,在 x64 机器上,进程 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]

请注意,据我所知,上述内容并非该工具的官方使用,并且可能会停止与新版本一起使用。当然,PerfView 可以做的不仅仅是强制 GC(开始 here )。

在内部,上面使用 ICorProfilerInfo::ForceGC CLR 附带的分析接口(interface)/方法( source 。因此,为此目的编写一个“更简单”/“独立”工具并非完全不可能。尽管如此,任务还是很重要。

更新:PerfView 本身现在是 open source以及上面提到的工具 is part of it 。如果你好奇的话。

更新:我滚动了 my own使用上述技术的版本。对我有用,但可能并不包罗万象。

关于.net - 从命令行运行垃圾收集器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3594069/

相关文章:

c# - 检查 `object[] args` 是否满足委托(delegate)实例?

python - 如何将lxml升级为python2.6

python-3.x - 了解 CPython 垃圾收集世代

c# - 在线程中调用UI应用程序

memory - 为什么 Dart 一段时间后变得很慢?

.net - 加载 psscor4 : The garbage collector data structures are not in a valid state for traversal 时

.net - 通过 Entity Framework 将实体插入到具有默认值列的表中

c# - TaskFactory 内部任务永远不会被执行并且始终处于 WaitingForActivation 状态

.net - .Net SQLConnection 类是否有更安全的替代方案?

java - 如何确定 Scala 中调用的 JAR 的名称?