简体   繁体   English

使用ANTS Memory Profiler在ASP.NET网站中查找内存泄漏?

[英]Finding memory leak in ASP.NET website with ANTS Memory Profiler?

I have a ASP.NET FRAMEWORK 4.0 website that has a memory leak. 我有一个存在内存泄漏的ASP.NET FRAMEWORK 4.0网站。 To find it I have installed ANTS Memory Profiler. 为了找到它,我已经安装了ANTS Memory Profiler。

This is what I do : 这就是我要做的:

  1. Host website in IIS7 在IIS7中托管网站
  2. Start Ants Memory Profiler 8.1 启动Ants Memory Profiler 8.1
  3. Set the we are profiling a IIS website and state the URL to this webpage (built in release) 设置我们正在分析的IIS网站,并声明该网页的URL(内置版本)
  4. Start test and let the webpage startup (a lot of caching so about 1 min) 开始测试并让网页启动(大量缓存大约需要1分钟)
  5. Take Memory Snapshot when first page is loaded and stable 加载第一页并稳定后获取内存快照
  6. Reload first page A LOT and see the memory raise from 110 MB (Private Bytes/Working Set -Private) to 270 MB 重新加载第一页的LOT,然后查看内存从110 MB(专用字节/工作集-专用)增加到270 MB
  7. Visit a lot of pages on the webpage and see it raise to 360 MB 访问网页上的许多页面,看到它增加到360 MB
  8. Push it some more and no more raise is done 多推一点,不再加注
  9. Take Memory Snapshot and click Class list (check Classes with source) 获取内存快照,然后单击“班级”列表(选中“带有源的班级”)

This will show classes that are still kept for example 这将显示仍保留的类,例如

sites_mypage_default_asx - 10 320 bytes and 10 live instances
usercontrols_common_pagehead_ascx - 928 bytes and 4 live instances

and so on 等等

I belive/hope that these are the classes that will be cleaned by the GC 我相信/希望这些是GC将清除的类

But this is not where the large foot print is, I have to uncheck the Classes with source to get the really large one. 但这不是大面积的地方,我必须取消选中带源代码的类以获得真正的大类。 For example(sorted on Live size(byte) 例如(按实时大小(字节)排序

string - 1 890 292 bytes
RuntimeMethodInfo - 990 976 bytes
RuntimePropertyInfo - 604 136 bytes
Hastable+bucket[] - 413 712 bytes

and so on. 等等。

The problem is that there is not much I can do about these, when opening Instance relation graph I will only see System. 问题在于,我对此无能为力,在打开“实例”关系图时,我只会看到“系统”。 classes and there is no information about where thay are hold in my website. 上课,没有关于我的网站在哪里的信息。

When the classes with source was checked I however found a big memory leak that could be fixed(this was before the above run). 当检查带有源的类时,我发现一个大的内存泄漏可以解决(在上面的运行之前)。

But I do not know how to take the next step? 但是我不知道下一步该怎么做? Why is my website still taking up 350 MB? 为什么我的网站仍然占用350 MB? 350 MB with data is a lot of data and I can´t see that I cache this much data!? 350 MB的数据是很多数据,我看不到我缓存了这么多数据!

What should be my next step? 我下一步应该怎么做?

It not must have be a memory leak, just the memory pressure is not enough for the Garbage Collector to make more comprehensive job. 它不一定是内存泄漏,只是内存压力不足以使垃圾回收器执行更全面的工作。 To fully investigate this issue and check if it is a real memory leak you should make a long-running load test of your web page with average traffic. 要全面调查此问题并检查是否是真正的内存泄漏,您应该使用平均流量对您的网页进行长时间的负载测试。 You can use Visual Studio Ultimate Load Testing if you are luck to have it or open source LoadUI project. 如果幸运的话,可以使用Visual Studio Ultimate负载测试,也可以使用开源LoadUI项目。 During this test observe Performance Counters: 在此测试期间,请观察性能计数器:

  • .NET Memory group counters, especially # Bytes in all Heaps and all Gen # heap size , .NET内存组计数器,尤其是# Bytes in all Heaps和所有Gen # heap size # Bytes in all Heaps
  • Process : Working Set and Process : Private bytes Process : Working SetProcess : Private bytes

After few hours of such test you will clearly see a trend of memory consumption. 经过几个小时的测试,您将清楚地看到内存消耗的趋势。 It might be that it will be released periodically if some threshold is exceeded. 如果超过某个阈值,则可能会定期释放它。 But if a memory consumption will grow all the time, you will have a more probable assumption of the memory leak. 但是,如果内存消耗一直在增长,那么您将有一个更可能的内存泄漏假设。 Then take a full memory dump of w3wp process at the end of memory leak and try to investigate it further. 然后在内存泄漏结束时对w3wp进程进行完整的内存转储,并尝试进一步调查。

As I am a big fan of WinDbg (it is faster, more detailed and cheaper than any GUI-based commercial tool), I suggest you to use it. 因为我是WinDbg的忠实拥护者 (它比任何基于GUI的商业工具都更快,更详细且更便宜),所以建议您使用它。 Use it with Psscor2 or Psscor4 extension (depending version used by you application). Psscor2Psscor4扩展名一起使用(取决于应用程序使用的版本)。 After setting up the debugging environment (installing WinDbg and copying to its folder Psscor files), create a dump of the process. 设置调试环境(安装WinDbg并将其复制到其文件夹Psscor文件)之后,创建该过程的转储。 You can do it easily for example with help of Procdump tool: 您可以轻松地做到这一点,例如借助Procdump工具:

procdump -ma <PID>

Then load dump using File -> Open Crush Dump option. 然后使用File-> Open Crush Dump选项加载转储。 Load appropriate version of Psscor: 加载适当的Psscor版本:

.load psscor4

Then execute command to download symbols from Microsoft servers (if needed), make sure that you have an internet connection: 然后执行命令以从Microsoft服务器下载符号(如果需要),请确保您具有Internet连接:

!symfix

And from now you should have access to plenty very interestings command (look for !help to list them). 从现在开始,您应该可以访问很多非常有趣的命令(寻找!help列出它们)。 To see memory usage per type: 要查看每种类型的内存使用情况:

!dumpheap -stat

Which will result in a long list of types and their memory usage sorted ascending: 这将导致一长串类型及其内存使用情况按升序排序:

...
0x79b56d84    297,724    12,308,164 SomeNamespace.SomeObject
0x6983ad6c      1,177    19,751,856 SomeNamespace.SomeClass[]
0x79ba4aa0      6,544    46,300,516 System.Byte[]
0x001027a0        527    69,152,092      Free
0x79b9fb08  1,127,896    82,456,640 System.String

To see overall memory usage ( iu means that also unrooted objects will be included): 要查看整体内存使用情况( iu表示还将包括未root用户的对象):

!heapstat -iu

Heap             Gen0         Gen1         Gen2          LOH
Heap0         6594540      1488744     24322236     19164192
Heap1         8360908       951312     30822196     14358048
Heap2         8207144       386488     23198448     16078256
Heap3         4299844       453440     36015332     16125560
Total        39615576      5301708    179028460     93254272

Free space:                                                 Percentage
Heap0         4868516           12         3512      8692736SOH: 15% LOH: 45%
Heap1         7221256           12        66200      5232904SOH: 18% LOH: 36%
Heap2         7518052           12          520      7677824SOH: 23% LOH: 47%
Heap3         3578232           12      6606504      4098640SOH: 24% LOH: 25%
Total        28807516           72      8353592     31990912

Unrooted objects:                                           Percentage
Heap0         1688812       258828      8905748      4019992SOH: 33% LOH: 20%
Heap1         1052548       270796      9983932      5625984SOH: 28% LOH: 39%
Heap2          503560       267112      7697632      4596792SOH: 26% LOH: 28%
Heap3          571776       235440      8453980      5205176SOH: 22% LOH: 32%
Total         9691432      2179788     53539772     32143328

This information for sure will lead you to some conclusions, but further investigation is obviously possible so do not hesitate to ask. 这些信息肯定会导致您得出一些结论,但是显然可以进行进一步的调查,所以请不要犹豫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM