创建大量线程时的.Net内存泄漏

编程入门 行业动态 更新时间:2024-10-15 02:32:35
本文介绍了创建大量线程时的.Net内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个应用程序,随着时间的推移会创建很多线程.我注意到,内存使用量随着运行的增加而增加,最终耗尽内存.但是,相同的代码不会在我的同事的环境中泄漏内存.我们都有相同的版本.我能够使用以下示例代码重现该问题,该示例代码不会在同事的笔记本电脑上泄漏,但在我的笔记本电脑上泄漏.

public static void Main(string[] args) { Console.WriteLine("Version " + Environment.Version.ToString()); if (Environment.Is64BitProcess) Console.WriteLine("64"); else Console.WriteLine("32"); while(true) { Thread t = new Thread(() => { Thread.Sleep(1); }); t.IsBackground = true; t.Start(); Thread.Sleep(1); } }

当我运行上面的命令时,它会打印以下内容

Version 4.0.30319.18063 32

在Visual Studio 2012中,该项目的目标框架是 framework 4.5. 该项目使用以下配置泄漏内存

Project Properties -> Build Platform target: Any CPU Prefer 32-bit: checked

如果我未选中首选32位",则不会泄漏.

泄漏内存的另一种配置是

Project Properties -> Build Platform target: x86 Prefer 32-bit: disabled

在笔记本电脑上泄漏的可执行文件不会在同事的笔记本电脑上泄漏.

我使用CLR Profiler来查找内存泄漏,但是它没有显示任何泄漏的信息.但是我确实看到Windows资源监视器中的工作集增加了大约1 MB/秒.

是什么导致我的环境而不是我的同事的内存使用率在32位模式下增加?

解决方案

我已阅读所有评论,担心我的评论会在那里丢失,所以请尝试回答.

使用为.NET应用程序,JetBrains dotMemory或ANTS或除WinDBG或Task Manager或其他本机内存工具之外的其他工具而设计的内存分析器.

您可以使用所选探查器的实时图表来比较应用程序在您和您同事的笔记本电脑上的表现. 我想您会发现笔记本电脑上的内存使用量不断增加-仅获取内存快照,并查看内存中有多少线程对象,哪些对象占用了大部分内存,并调查它们为什么以及由谁保留在内存中.

I have an application that creates lot of threads over time. I noticed that memory usage grows as it runs and eventually runs out of memory. But the same code doesn't leak memory on my coworker's environment. We both have same version. I was able to reproduce the issue with the following sample code which doesn't leak on my coworker's laptop but does on my laptop.

public static void Main(string[] args) { Console.WriteLine("Version " + Environment.Version.ToString()); if (Environment.Is64BitProcess) Console.WriteLine("64"); else Console.WriteLine("32"); while(true) { Thread t = new Thread(() => { Thread.Sleep(1); }); t.IsBackground = true; t.Start(); Thread.Sleep(1); } }

When I run the above, it prints the following

Version 4.0.30319.18063 32

In Visual Studio 2012 the target framework for the project is framework 4.5. The project leaks memory with following configuration

Project Properties -> Build Platform target: Any CPU Prefer 32-bit: checked

If I unchecked Prefer 32-bit, it doesn't leak.

Another configuration that leaks memory is

Project Properties -> Build Platform target: x86 Prefer 32-bit: disabled

The resulting executable that leaks on my laptop doesn't leak on my coworker's laptop.

I used CLR Profiler to find memory leaks but it doesn't show anything that's leaking. But I do see that working set in windows resource monitor increases by about 1 MB/sec.

What is causing the memory usage to increase in 32-bit mode on my environment but not my coworker's?

解决方案

I've read all comments and afraid that my comment will be lost there, so try to answer.

Use memory profiler designed for .NET applications, JetBrains dotMemory or ANTS or whatever but WinDBG or Task Manager or other native memory tools.

You can compare how app behaves on your and your colleague's laptops using realtime chart of chosen profiler. I guess you will see that memory usage constantly increases on your laptop - just get a memory snapshot and see how many Thread objects are in memory, what objects take most of the memory and investigate why and by whom are they held in memory.

更多推荐

创建大量线程时的.Net内存泄漏

本文发布于:2023-11-13 02:30:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583175.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   内存   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!