为什么当初始化一个二维数组不.NET分配内存? [英] why doesn't .net allocate memory when initialize a 2d array?

查看:132
本文介绍了为什么当初始化一个二维数组不.NET分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 var a = new double[7000,7000];

 FillValue(a,3);



看来.NET不实际分配的内存 A 在执行第一行之后。只有当它运行 FillValue 呼叫做它的慢慢吃内存。 (这大约是400MB)

It seems .Net doesn't actually allocate any memory to a after executing the first line. Only while it is running the FillValue call does it gradually eat the memory. (which is around 400MB)

谁能为我提供有关它的更多详细信息?我以为 A 默认初始化后填充0,怎么可能不采取任何内存呢?

Can anyone provide me with more details regarding it? I thought a is filled with 0 after default initialization, how could it take no memory at all?

推荐答案

有两种方法在Windows中分配内存:为储备和提交记忆

There are two ways to 'allocate memory' in Windows: to 'reserve' and to 'commit' memory.

任务管理器只显示物理内存使用率的历史;在.NET VM显然只使用保留内存,当你分配一个数组,然后返回并提交了习惯的部分。

The task manager only shows "Physical Memory Usage History"; the .NET VM apparently uses only reserved memory when you allocate an array, and then goes back and commits the parts that get used.

这样你就可以保留内存实际上不服用起来,这是更有效的,并且预留存储器,根据MSDN,保留的范围内的过程中的虚拟地址空间的不分配内存或在磁盘上的寻呼文件中的任何实际的物理存储。这就是为什么任务管理器不显示它。

This way you can reserve memory without actually taking it up, which is more efficient, and reserving memory, according to MSDN, "reserves a range of the process's virtual address space without allocating any actual physical storage in memory or in the paging file on disk". That's why Task Manager doesn't show it.

您可以阅读更多关于它的的 虚拟 MSDN上的页面。

You can read more about it on the VirtualAlloc page on MSDN.

这是一个实现细节的,所以你不应该依赖于它或任何东西。单声道的VM,例如,很可能表现不同。

This is an implementation detail though, so you shouldn't rely on it or anything. The Mono VM, for example, is likely to behave differently.

这篇关于为什么当初始化一个二维数组不.NET分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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