asp.net 应用程序中的内存泄漏 - W3WP 和 gen 2 堆持续增长,直到 AppPool 回收
我们有一个大型 ASP.NET 应用程序正在泄漏内存。 Perfmon 显示此泄漏发生在托管内存中,因为 W3WP 专用字节的增长速度与所有堆中的字节增长速度相同。我还可以看到第 2 代垃圾收集正在运行,但第 2 代堆大小继续增长。
我获取了内存转储并在 WinDbg 中进行了分析,可以看到大量多种类型的对象。弦是最大的类型,20% 的弦由 51 个物体组成。
转储这些大字符串会显示控件或整个页面的输出 html。在这些上运行 !gcroot 显示根对象的类型为 System.Text.RegularExpressions.Regex 或 System.Web.RegularExpressions.GTRegex。
关于可能发生的事情或我如何进一步调查有什么想法吗?
谢谢,西蒙
We have a large asp.net application that is leaking memory. Perfmon shows that this leak is in managed memory as W3WP private bytes grows at the same rate as bytes in all heaps. I can also see that Gen 2 garbage collections are running but the Gen 2 heap size continues to grow.
I took a memory dump and analysed in WinDbg and can see a very large number of objects of lots of types. Strings are the biggest type and 20% of the size of the strings is made from 51 objects.
Dumping these large strings shows outputted html either from controls or entire pages. Running !gcroot on these shows the root objects being of type System.Text.RegularExpressions.Regex or System.Web.RegularExpressions.GTRegex.
Any ideas of what could be happening or how I can investigate further?
Thanks, Simon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何使用内存分析器,例如 dotTrace Memory 或 ANTZ 内存分析器?这两款产品均提供限时试用版。
How about using a memory profiler such as dotTrace Memory or ANTZ Memory Profiler? Both products are available as a time-limited trial version.
字符串是堆上最常见的类型,这一点并不奇怪。例如,如果您有 10 个 HashSet,每个 HashSet 包含 1000 个字符串,则转储将显示您的堆上有 10 个 HashSet,但包含 100 000 个字符串。许多对象包含一个或多个字符串。因此,转储中显示的字符串数量是堆上所有对象的所有字符串的总和,这往往会很多。
但是,如果堆上有很多 System.Text.RegularExpressions.Regex,那么这很可能是内存问题的根源。 .NET 中的正则表达式往往会占用大量资源。因此,我的建议是检查您的代码并尝试查找任何过度使用正则表达式的情况。另外,请确保对 Regex 对象的任何引用都得到处理,也就是说,对 Regex 对象的引用不会保持活动状态。这样,垃圾收集器可以确保正则表达式对象被正确释放。
祝你好运!
That strings are the most common type on your heap is not strange at all. If you for example have 10 HashSet's containing 1000 strings each, the dump will show that you have 10 HashSet's on your heap, but 100 000 strings. Many objects contain one or several strings. Thus, the number of strings shown in the dump is the sum of all strings from all objects on the heap, which tend to be a lot.
However, if you have alot of System.Text.RegularExpressions.Regex on your heap, that can very well be the root to your memory problemts. Regex in .NET tend to take a lot of resources. Hence, my advice is that you go through your code and try to find any excessive use of regex. Also, make sure that any references to Regex objects are being taken care of, that is to say, that the references to the Regex objects are not kept alive. That way, the Garbage Collector can make sure the Regex objects are deallocated properly.
Good luck!
理论上,在不使用非托管资源的情况下,应该很难在 ASP.NET 中引起内存泄漏。如果一切都是单线程的,那么当页面生命周期完成时,对托管资源的所有引用都应该可以自由地进行垃圾收集。您是否会触发工作线程来执行任何操作,并且这些线程是否会在页面生命周期结束后继续存在?或者您是否有任何长时间运行的进程公开为 Web 方法,可以通过异步方式触发,并且需要很长时间才能运行并被重复调用,直到内存已满?
In theory it should be quite dificult to cause a memory leak in asp.net without using unmanaged resources. If everything is single threaded then all references to managed resources should be free to be garbage collected when the page life cycle is complete. Are you firing off worker threads to do anything and are these threads continuing to live beyond the life of the page? Or do you have any long running processes exposed as web methods that can be fired off by asynchounsly and are just taking a long time to run and being called repeatedly until the memory is full?