每个线程的内存管理在C# [英] Per-thread memory management in C#

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

问题描述

从继续 http://stackoverflow.com/questions/2902984/understanding讨论-vs2010-C-平行分析,结果的,但更重要的是:



我有很多,在并行工作线程(使用的Parallel.For /每),其中用于小班许多内存分配。



这创建全局内存分配器线程争。



有没有一种方法来指导.NET从此池中预先分配为每个线程内存池,做所有的分配?



目前我的解决方案是我自己实现的内存池,它可以帮助了很多,但是没有效率(T型,这是本线程之间循环的对象的全局分配阵列),因为:



<醇>
  • 我不能指示.NET从一个特定的内存分配片

  • 我仍然需要调用的新的的多次分配的内存池。



  • 谢谢,结果


    解决方案

    我搜索了两天试图找到一个答案,你有同样的问题。答案是你需要设置垃圾收集模式为服务器模式。默认情况下,垃圾收集模式设置为工作站模式。
    垃圾收集设置为服务器模式会导致托管堆分割成单独管理的部分,一个每个CPU。
    。要做到这一点,你需要一个配置设置添加到您的app.config文件

     <运行> 
    <启用gcServer =真/>
    < /运行>

    这是我的12核皓龙6172的速度差别是巨大的!


    Continuing the discussion from http://stackoverflow.com/questions/2902984/understanding-vs2010-c-parallel-profiling-results but more to the point:

    I have many threads that work in parallel (using Parallel.For/Each), which use many memory allocations for small classes.

    This creates a contention on the global memory allocator thread.

    Is there a way to instruct .NET to preallocate a memory pool for each thread and do all allocations from this pool?

    Currently my solution is my own implementation of memory pools (globally allocated arrays of object of type T, which are recycled among the threads) which helps a lot but is not efficient because:

    1. I can't instruct .NET to allocate from a specific memory slice.
    2. I still need to call new many times to allocate the memory for the pools.

    Thanks,
    Haggai

    解决方案

    I searched for two days trying to find an answer to the same issue you had. The answer is you need to set the garbage collection mode to Server mode. By default, garbage collection mode set to Workstation mode. Setting garbage collection to Server mode causes the managed heap to split into separately managed sections, one-per CPU. To do this, you need to add a config setting to your app.config file.

    <runtime>
       <gcServer enabled="true"/>
    </runtime>
    

    The speed difference on my 12-core Opteron 6172 was dramatic!

    这篇关于每个线程的内存管理在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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