Triggering a .NET garbage collection externally

2019-06-15 02:23发布

Is there a way to trigger a garbage collection in a .NET process from another process or from inside WinDBG?

There are the Managed Debugging Assistants that force a collection as you move across a native/managed boundary, and AQTime seems to have button that suggests it does this, but I can't find any documentation on how to do it.

4条回答
放荡不羁爱自由
2楼-- · 2019-06-15 02:29

If you expose a function/object via remoting, that could be done quite easily.

查看更多
叼着烟拽天下
3楼-- · 2019-06-15 02:32

Answered in another question :

Basically, use PerfView:

PerfView.exe ForceGC [ProcessName | Process ID] /AcceptEULA

It's not intended for production use.

查看更多
戒情不戒烟
4楼-- · 2019-06-15 02:47

John Cocktoastan's answer to use GC.Collect when in Visual Studio is the best option if there.

I still can't find an alternative to actually do the collection under WinDBG but taking a step back to problem of "How much memory is reclaimable?" (see my comment to John's answer) I think there is an alternative by using a scripted (PowerDBG?) search via some combination of !DumpHeap and !GCRoot to find the non-rooted handles and total the space used (basically emulate the algorithm that the GC would do using the debugger). But since thinking of this I haven't had one of these bugs so haven't tried to write the code to do it.

查看更多
混吃等死
5楼-- · 2019-06-15 02:51

Well... there's the immediate window. If you have the luxury of attaching to the process, I supposed you could manually GC.Collect in the immediate window.

Bigger question: why would you want to manually induce GC.Collect? It's a nasty habit, and indicative of much bigger design issues.

查看更多
登录 后发表回答