C# WPF 中的内存泄漏 [英] Memory Leaks in C# WPF

查看:41
本文介绍了C# WPF 中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用一些建议来追踪 C# 中内存泄漏的原因.我明白什么是内存泄漏,我明白为什么它们会出现在 C# 中,但我想知道你过去使用过哪些工具/策略来解决它们?

I could use some advice on tracking down the cause of memory leaks in C#. I understand what is a memory leak and I get why they occur in C# but I'm wondering what tools/strategies have you used in the past to resolve them?

我正在使用 .NET Memory Profiler,我发现我的一个巨大的主要对象在我关闭它管理的窗口后仍留在内存中,但我不知道该怎么做才能严重地影响到它的所有链接.

I am using .NET Memory Profiler and I've found that one of my huge main objects is staying in memory after I close the window it manages but I'm not sure what to do to severe all links to it.

如果我不够清楚,只需发布​​一个问题的答案,我会编辑我的问题作为回应.谢谢!

If I'm not being clear enough just post an answer with a question and I'll edit my question in response. Thanks!

推荐答案

进入调试器,然后在立即窗口中输入:

Break into the debugger and then type this into the Immediate window:

.load C:WindowsMicrosoft.NETFrameworkv2.0.50727sos.dll

sos.dll 的路径各不相同.找出正确路径的方法是在模块"窗格中查找 mscorwks.dll.无论从何处加载都是 sos.dll 的正确路径.

The path to sos.dll varies. The way to find out the correct path is to look for mscorwks.dll in the Modules pane. Wherever that is loaded from is the correct path for sos.dll.

然后输入:

System.GC.Collect()

这将确保收集任何无法访问的内容.然后输入:

That will ensure anything not reachable is collected. Then type this:

!DumpHeap -type <some-type-name>

这将向您显示所有现有实例的表格,以及地址.您可以像这样找出使实例保持活动状态的原因:

This will show you a table of all existing instances, with addresses. You can find out what is keeping an instance alive like this:

!gcroot <some-address>

这篇关于C# WPF 中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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