C#:新的[]似乎并不在调试分配内存 [英] C#: new[] does not seem to allocate memory in Debug

查看:122
本文介绍了C#:新的[]似乎并不在调试分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试分配一大块内存作为一个字节数组(400MB)

I attempt to allocate a large chunk of memory as a byte array (400MB)

    public static void Main(string[] args)
    {
        const int MegaByte = 1048576;

        byte[] bytes = new byte[400 * MegaByte];

        for (int i = 0; i < bytes.Length; i++)
        {
            bytes[i] = 3;
        }

        Console.WriteLine("Total array length : {0}", bytes.Length);
        Console.WriteLine("First byte = '{0}' Last byte = '{1}'", bytes[0], bytes[bytes.Length - 1]);
        Console.Read();
    }

输出:

Total array length : 419430400
First byte = '3' Last byte = '3'

正如预期的那样我看到在Windows任务管理器正在使用,当我刚分配的内存400MB内存一个大的跳跃。但是如果我更换forloop使用情况,只是用第一和最后一个字节:

As expected I see a large jump in Windows Task Manager for the memory that is being used, when I just allocated 400MB of memory. However if I replace the forloop usage to just using first and last bytes:

    public static void Main(string[] args)
    {
        const int MegaByte = 1048576;

        byte[] bytes = new byte[400 * MegaByte];

        bytes[0] = 0;
        bytes[bytes.Length - 1] = 2;

        Console.WriteLine("Total array length : {0}", bytes.Length);
        Console.WriteLine("First byte = '{0}' Last byte = '{1}'", bytes[0], bytes[bytes.Length - 1]);
        Console.Read();
    }     

输出:

Total array length : 419430400
First byte = '0' Last byte = '2'

中使用的存储器中没有大的跳跃,即使分配数组的长度被认为是相同的观察。我有一个假设,如果我们建立在调试也不会有这样的优化。是否有一些优化那些在调试完成建造或做当IL被编译这样的事情得到在较低水平优化?

No large jump in used memory is observed even though the length of the allocated array is said to be the same. I had an assumption that if we build in Debug there won't be such optimizations. Are there some optimizations that are done during 'Debug' builds or do such things get optimized at a lower level when IL gets compiled?

推荐答案

调试/发布模式的优化仅用于指导优化(IL - >本地code编译,内联,指令重新排序,等等 - 一切除const折叠,它总是使用)。这不会改变内存的方式进行管理,它只是改变了你的code的流程和方式访问存储器。你可以看到这个当你看到IL code和比较,为相应的汇编code:大部分IL code只定义内存的方式访问/使用;分配和释放总是由GC管理(与速记如 newobj newarr 为)。

Debug / Release mode optimizations are only instruction optimizations (IL -> native code compilation, inlining, reordering of instructions, etc - everything except const folding, which is always used). This doesn't change the way memory is managed, it just changes the flow of your code and the way memory is accessed. You can see this when you look at IL code and compares that to the corresponding assembler code: most IL code just defines the way memory is accessed/used; allocation and deallocation is always managed by the GC (with shorthands such as newobj and newarr for that).

您可以影响记忆行为的唯一方法是通过更改设置来影响GC。 GC的相关设置可以在这里找到: http://msdn.microsoft.com/en-us/library/6bs4szyc(v=vs.110).aspx

The only way you can influence memory behavior is to influence the GC by changing the settings. The relevant settings of the GC can be found here: http://msdn.microsoft.com/en-us/library/6bs4szyc(v=vs.110).aspx .

所以,在所有的情况下,内存是由GC,这(我猜使用释放calloc ),并释放内存从内核抓住内存(免费)内核。在你的情况,内存分配(地址空间保留给数据),但没有提交。当你访问一个页面(4 KB),它的承诺,它显示了。

So, in all cases, memory is managed by the GC, which grabs memory from the kernel (I guess using calloc) and releases memory (free) to the kernel. In your case, memory is allocated (address space is reserved for data), but not committed. Once you access a page (4 KB), it's committed and it shows up.

这篇关于C#:新的[]似乎并不在调试分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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