在.NET数组分配 [英] Array Allocation in .NET

查看:164
本文介绍了在.NET数组分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在的问题是关于阵列在.NET中的分配。我下面的示例程序,其中最大的数组我能得到的是长度。我增加长度+1它给内存不足的异常。但是如果我把长度和删除的评论我能够分配2个不同的大的阵列。这两个数组是2 GB的总内存比虚拟内存也少了少了.NET允许对象的大小。有人可以把任何想法?

 
类节目
    {
        静态INT长度= 203423225;
        静态双重[] D =新的双[长度];
        //静态INT [1 =新的INT [1500];
        静态无效的主要(字串[] args)
        {

            Console.WriteLine((的sizeof(双)*(双)长度)/(1024 * 1024));

            Console.WriteLine(d.Length);
            //Console.WriteLine(i.Length);
            Console.WriteLine(Process.GetCurrentProcess()VirtualMemorySize64.ToString());
        }
    }
 

解决方案

一个32位的过程中,必须从它的可用地址空间中的阵列分配虚拟内存。默认情况下,2千兆字节。其中包含两个code和数据的组合。分配是从现有的分配之间的孔进行。

这样的分配总是失败不是因为没有更多的虚拟内存离开了,​​他们失败,因为可用的孔不够大。而你要求一个大洞,得到1.6 jiggabytes是非常罕见的,只能在非常简单的程序,请不要将任何额外的DLL。 A基差的DLL是一个很好的方式来减少一个大洞一分为二,大大减少了这种分配成功的可能性。较为典型的作品 - 上的先试分配大约是650兆字节。因为有可用的另一个孔的第二分配并没有失败。赔率下去大大程序后已运行了一段时间,地址空间得到了碎片。 A 90 MB的分配可能会失败。

您可以在虚拟内存地址空间是如何刻上去的​​与Sysinternals的的VMMap实用程序的见解。

有一个简单的解决方法是将EXE项目的平台目标设置设为值为anycpu,并在64位操作系统上运行的程序。则其可寻址的虚拟内存空间可采空区,你将只能由分页文件的最大允许大小和.NET 2 GB的对象大小上限的限制。多数民众赞成在讨论.NET 4.5的新的限制< gcAllowVeryLargeObjects> 配置元素。即使是32位程序可以在64位操作系统editbin.exe的/ LARGEADDRESSAWARE选项可用的4 GB的32位地址空间的优势,你必须在生成后事件运行。

The question is regarding the allocation of arrays in .net. i have a sample program below in which maximum array i can get is to length. I increase length to +1 it gives outofMemory exception. But If I keep the length and remove the comments I am able to allocation 2 different big arrays. Both arrays are less the .net permissible object size of 2 gb and total memory also less than virtual Memory. Can someone put any thoughts?

 
class Program
    {
        static int length = 203423225;
        static double[] d = new double[length];
        //static int[] i = new int[15000000];
        static void Main(string[] args)
        {

            Console.WriteLine((sizeof(double)*(double)length)/(1024*1024));

            Console.WriteLine(d.Length);
            //Console.WriteLine(i.Length);
            Console.WriteLine(Process.GetCurrentProcess().VirtualMemorySize64.ToString());
        }
    }

解决方案

A 32-bit process must allocate virtual memory for the array from the address space it has available. by default 2 gigabytes. Which contains a mix of both code and data. Allocations are made from the holes between the existing allocations.

Such allocations always fail not because there is no more virtual memory left, they fail because the available holes are not big enough. And you asking for a big hole, getting 1.6 jiggabytes is very rare and will only work on very simple programs that don't load any additional DLLs. A poorly based DLL is a good way to cut a large hole in two, drastically reducing the odds of such an allocation succeeding. The more typical works-on-the-first-try allocation is around 650 megabytes. The second allocation didn't fail because there was another hole available. The odds go down considerably after a program has been running for a while and the address space got fragmented. A 90 MB allocation can fail.

You can get insight in how the virtual memory address space is carved up for a program with the SysInternals' VMMap utility.

A simple workaround is to set the EXE project's Platform target setting to AnyCPU and run the program on a 64-bit operating system. It will have gobs of addressable virtual memory space available, you'll only be limited by the maximum allowed size of the paging file and the .NET 2 gigabyte object size limit. A limitation that's addressed in .NET 4.5 with the new <gcAllowVeryLargeObjects> config element. Even a 32-bit program can take advantage of the available 4 gigabyte 32-bit address space on a 64-bit operating system with the /LARGEADDRESSAWARE option of editbin.exe, you'll have to run it in a post-build event.

这篇关于在.NET数组分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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