c#线程内存使用情况 [英] c# thread memory usage

查看:96
本文介绍了c#线程内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习更多关于线程的知识,并使用以下代码(x64 平台构建)创建了一个相当简单的 WPF 应用程序

I was learning more about threading, and i created a rather simple WPF application with the following code (x64 platform build)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        for (var i = 0; i <= 20000; i++)
        {
            Thread thread = new Thread(Test);
            thread.IsBackground = true;

            thread.Start();
        }
    }

    public void Test()
    {
        Thread.Sleep(20000);
    }
}

当我运行这段代码时,当所有线程都在运行/休眠时,进程需要大约 560MB 的 RAM.

When i run this code, process takes approximately 560MB of RAM while all threads are running / sleeping.

完成后,进程使用量降至约 125 MB 的 RAM.

Upon it's completion, process usage is down to aprox 125 MB of RAM.

我的问题是,当应用程序本身(没有线程示例)仅使用 30 MB 的 RAM 时,为什么此时进程使用 125 MB 的 RAM?

My question is, why does process use 125 MB of RAM at that point, when application itself (without thread example) is using only 30 MB of RAM?

它是否让某些线程保持活动状态以供可能的重新/使用或其他正在发生的事情?

Does it keep some of the threads alive for possible re/usage or something else is going on?

由于一些关于如何改进此代码的建议,我想指出我不是在寻求改进它的方法,而是要查明这种行为的原因.

编辑 2:

这与线程无关,但我曾尝试过在内存中有一个很大的 string 列表的案例,但它没有产生相同的结果.当 list 完全加载到内存中时,大约需要 1.3 GB 的内存,但是当 list 设置为 NULL 并调用 GC.Collect() 后,内存使用量下降了回到预期的 30 MB.

This is not a thread related, but I have tried a case with a large string list in memory, and it did not produce same results. When list was fully loaded in memory, it took about 1.3 GB of memory, but after list was set to NULL, and GC.Collect() was called, memory usage dropped back to 30 MB as expected.

代码:

public partial class MainWindow : Window
{
    List<string> stringArray = new List<string>();

    public MainWindow()
    {
        InitializeComponent();


        for (var i = 0; i <= 100000000; i++)
        {
            //Thread thread = new Thread(Test);
            //thread.IsBackground = false;

            //thread.Start();

            stringArray.Add("Some very long string to pump up the memory volume 2 reloaded");
        }

        stringArray = null;
        GC.Collect();
    }



}

推荐答案

每个线程使用的部分内存在线程完成时被释放.这就是所有后台线程完成后内存消耗从 560 MB 下降到 125 MB 的原因.

Part of the memory used by each thread is deallocated when the thread completes. That's why the memory consumption drops from 560 MB to 125 MB when all background threads complete.

剩余的内存分配在托管堆上,由垃圾收集器释放.

The rest of the memory is allocated on the managed heap and will be freed by garbage collector.

当您的应用程序处于空闲状态时,不会分配新内存,因此不需要运行垃圾收集器.您在评论中提到使用 GC.Collect() 强制 GC 没有帮助,但请记住 Thread 已实现终结器,因此它会在第一个收藏.你将不得不使用

When your application sits idle, no new memory is allocated, so there is no need for the garbage collector to run. You mentioned in a comment that forcing GC using GC.Collect() didn't help, but keep in mind that Thread has a finalizer implemented, so it will survive the first collection. You will have to use

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

确保内存被释放.

这应该将内存使用量减少到启动线程之前的水平.

That should reduce the memory usage to the level before starting the threads.

这篇关于c#线程内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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