我怎样才能腾出一个Parallel.Task使用的内存? [英] How can I free-up memory used by a Parallel.Task?

查看:243
本文介绍了我怎样才能腾出一个Parallel.Task使用的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,做了内存密集型模拟。下面,我已经写了复制我有问题的小型控制台应用程序。

I have a program that does a memory intensive simulation. Below I've written a small console application that replicates the problem I'm having.

class Program {
    static void Main(string[] args) {
        var t = new Task(() => DoMemoryHog(20000000));
        t.Start();
        t.Wait();
        t.Dispose();
        t = null;
        GC.Collect();
        Console.WriteLine("Done");
        Console.ReadLine();
    }

    static void DoMemoryHog(int n) {
        ConcurrentBag<double> results = new ConcurrentBag<double>();

        Parallel.For(0, n, (i) => {
            results.Add(Math.Sqrt(i.GetHashCode()));
        });
    }
}

当我运行这个程序,我可以看到量所使用的内存在Windows任务管理器增加,但是当任务完成后(和完成显示),内存不回去到原来的水平,只有当我关闭应用程序发生。

When I run the program, I can see the amount of used memory increasing in the windows task manager, but when the task is finished (and "Done" is displayed) the memory doesn't go back to it's original level, that only happens when I close the application.

有谁知道如何释放被并行任务使用的内存,而主应用程序继续运行?正如你所看到的,我已经尝试过处置它,设置它的引用为null,手动(你不应该做的,我知道)运行垃圾回收器。

Does anyone know how to free up memory used by a parallel task, while the main application keeps running? As you can see, I've already tried disposing it, setting it's reference to null and running the garbage collector manually (which you shouldn't do, I know).

推荐答案

我敢肯定你没有与你的内存有问题,.NET只是不收缩使用的内存,因此它可以在未来assigend。这样可以节省时间用于未来的内存分配。尝试重新运行在循环后完成它,我敢肯定,内存也不会增加。

I'm sure you don't have a problem with your memory, .NET just doesn't shrink the used Memory so it can be assigend in the future. This saves time for future memory allocation. Try to rerun the loop after it finished, i'm sure the memory wouldn't grow.

所以,请试试这个,我会在结果intrested <! / p>

So please just try this, i would be intrested in the outcome!

class Program {
    static void Main(string[] args) {
        Process currentProcess = Process.GetCurrentProcess();
        for (int i = 0; i < 10; i++) {
            var t = new Task(() => DoMemoryHog(20000000));
            t.Start();
            t.Wait();
            t.Dispose();
            t = null;
            GC.Collect();
            Console.WriteLine("Done" +i);
            Console.WriteLine("Memory: " + GC.GetTotalMemory(false));
            Console.WriteLine("Paged: " + currentProcess.PagedMemorySize64);
            Console.WriteLine("-------------------------");
        }
        Console.ReadLine();
    }

    static void DoMemoryHog(int n) {
        ConcurrentBag<double> results = new ConcurrentBag<double>();

        Parallel.For(0, n, (i) =>
        {
            results.Add(Math.Sqrt(i.GetHashCode()));
        });
    }
}



使用这两种方法来打印出内存使用情况:

Those two methods are used to print out memory usage:

GC.GetTotalMemory();
currentProcess.PagedMemorySize64;



我的输出是:

My output is:

Done0
Memory: 480080992
Paged: 520753152
-------------------------
Done1
Memory: 480081512
Paged: 520753152
-------------------------
Done2
Memory: 480082160
Paged: 520753152
-------------------------
Done3
Memory: 480083476
Paged: 520753152
-------------------------
Done4
Memory: 480083496
Paged: 520753152
-------------------------
Done5
Memory: 480083516
Paged: 520753152
-------------------------
Done6
Memory: 480083536
Paged: 520753152
-------------------------
Done7
Memory: 480084204
Paged: 520753152
-------------------------
Done8
Memory: 480084204
Paged: 520753152
-------------------------
Done9
Memory: 480085500
Paged: 520753152
-------------------------

据我可以看到这里没有记忆问题。物体被cleand向上和存储器被重用属性格式。 ?问题解决或

As far as i can see there is no memory problem here. The objects are cleand up and the memory is reused propery. Issue solved or?

专用字节的行为:

正如你可以看到GC正在收集的对象和释放内存,但它目前还没有释放,所以新的对象可以分配。

As you can see the GC is collecting the Objects and freeing the memory, but it's currently not releasing it, so new objects can be allocated.

这篇关于我怎样才能腾出一个Parallel.Task使用的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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