创建大量线程时的.Net内存泄漏 [英] .Net Memory Leak when creating lots of threads

查看:139
本文介绍了创建大量线程时的.Net内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,随着时间的推移会创建很多线程.我注意到,内存使用量随着运行的增加而增加,最终耗尽内存.但是,相同的代码不会在我的同事的环境中泄漏内存.我们都有相同的.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中,该项目的目标框架是.net 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 .net 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 .net 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内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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