我期待通过WPF应用程序寻找一个内存泄漏(使用蚂蚁内存分析器5.1),我不断看到一些页面和控件占用内存时,他们不应该。
I'm looking through a WPF application looking for a memory leak (using ANTS Memory Profiler 5.1) and I keep seeing some pages and controls taking up memory when they shouldn't be.
所以我去的对象保持图表,看看是什么让他们过来,我总是看到这样的每一页:
So I go to the Object Retention Graph and to see what is keeping them around, and I keep seeing this for every page:
的事情是,我的KeepAlive设置为false每一页上,我不认为这样的属性存在,对用户的控制。
The thing is, I have KeepAlive set to false on every page, and I don't think such a property exists on the user controls.
谁能告诉我,我应该找谁?这甚至是内存泄漏或者是一个WPF应用程序这是正常的行为呢?
Can anyone tell me what I should be looking for? Is this even a memory leak or is this normal behaviour for a WPF application?
是的,根据您提供什么,你有内存泄漏。当你找到了引用链,它不是在你的code,走最简单的方法是...反射。
Yes, according to what you've provided, you have a memory leak. When you found the references chain, and it's not in your code, the easiest way to go would be... Reflector.
图片说: JournalEntryKeepAlive._keepAliveRoot
字段保存引用的对象。让我们在反射器,看看这家伙是如何迷上我们的目标。
Image says: JournalEntryKeepAlive._keepAliveRoot
field holds a reference to the object. Let's go in Reflector and see how this guy is hooked with our object.
这一次,它是容易的,和所有的痕迹导致 NavigationService.MakeJournalEntry()
函数,然后到 NavigationService.IsContentKeepAlive()
。在这里,它是:
This time it was easy, and all traces lead to NavigationService.MakeJournalEntry()
function and then to NavigationService.IsContentKeepAlive()
. Here it is:
internal bool IsContentKeepAlive()
{
bool keepAlive = true;
DependencyObject dependencyObject = this._bp as DependencyObject;
if (dependencyObject != null)
{
keepAlive = JournalEntry.GetKeepAlive(dependencyObject);
if (!keepAlive)
{
PageFunctionBase base2 = dependencyObject as PageFunctionBase;
bool flag2 = !this.CanReloadFromUri;
if ((base2 == null) && flag2)
{
keepAlive = true;
}
}
}
return keepAlive;
}
现在你知道的规则。对象是保存在内存中,如果:
Now you know the rules. Object is kept in memory if:
这不是一个依赖对象; 在附加属性格式JournalEntry.KeepAlive是真实的; 这不是一个的PageFunction,它不能从乌里重新加载。在本次调查也可能是值得一读更多关于JournalEntry.KeepAlive物业MSDN上。
After this investigation it may be worth reading more about JournalEntry.KeepAlive property on MSDN.
此战略帮助我找到很多内存相关的昆虫。希望它可以帮助你太:)
This strategy helped me to find many memory-related insects. Hope it helps you too :).
PS:如果你跟上找到这个特殊的泄漏有问题,你可以粘贴最低code样品为我们重现它,给你更多的正确的答案
PS: If you keep having problem with finding this particular leak, you could paste minimal code sample for us to reproduce it and give you more proper answer.
干杯, Anvaka
Cheers, Anvaka