分配不使用的内存,除非初始化? [英] Memory allocated not used unless initialized?

查看:96
本文介绍了分配不使用的内存,除非初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续到我刚才问<一个问题href=\"http://stackoverflow.com/questions/8611198/understanding-memory-allocation-test-program-crashing\">here.

我创建了一个简单的程序来帮助自己理解的内存分配,的malloc()免费() 。注意注释掉免费行。我创建了一个故意的内存泄漏,所以我可以看的Windows报道的内存使用慢慢地膨胀到1GB。但后来我发现了一些陌生人。如果我注释掉循环正上方的自由线,让我不随机整数初始化我的存储块,看来空间实际上并没有得到宣称的程序,从操作系统。这是为什么?

当然,我还没有初始化它块,但我有人称它,所以不应在OS仍然可以看到该程序正在使用1GB,不论该国标被初始化?

 的#include&LT;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;无效alloc_one_meg(){
    为int * PMEG =(INT *)malloc的(250000 * sizeof的(INT));
    为int * p = PMEG;    INT I;
    //为(i = 0; I&LT; 250000;我++)/ *删除这个循环会导致内存不被使用的? * /
    // * P ++ = RAND();
    //自由((无效*)PMEG); / *删除此行导致内​​存泄漏! * /
}主要()
{
    INT I;
    对于(i = 0; I&LT; 1000;我++){
        alloc_one_meg();
    }
}


解决方案

分配的内存可以在两种状态在Windows:保留和COMMITED(见的文档<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887%28v=vs.85%29.aspx\"><$c$c>VirtualAlloc关于 MEM_RESERVE 储备范围内的进程的虚拟地址空间不分配在内存或磁盘上的页面文件的任何实际的物理存储。)。

如果您分配内存,但不使用它,它仍然在保留状态,操作系统不计为使用的内存。当您尝试使用它(无论是只写,或读写,我不知道,你可能想要做一个测试,找出),它变成COMMITED内存,操作系统把它看做用了。

此外,内存的malloc 将的的饱满的分配0(实际上它可能碰巧,但它不能保证) ,因为的你还没有初始化它。

This is a followup to the question I just asked here.

I've created a simple program to help myself understand memory allocation, malloc() and free(). Notice the commented out free line. I created an intentional memory leak so I can watch the Windows reported "Mem Usage" bloat slowly to 1GB. But then I found something stranger. If I comment out the loop just above the free line, so that I don't initialize my storage block with random ints, it appears that the space doesn't actually get "claimed" by the program, from the OS. Why is this?

Sure, I haven't initialized it the block, but I have claimed it, so shouldn't the OS still see that the program is using 1GB, whether or not that GB is initialized?

#include <stdio.h>
#include <stdlib.h>

void alloc_one_meg() {
    int *pmeg = (int *) malloc(250000*sizeof(int));
    int *p = pmeg;

    int i;
    // for (i=0; i<250000; i++) /* removing this loop causes memory to not be used? */
    //    *p++ = rand();
    // free((void *)pmeg); /* removing this line causes memory leak! */
}

main()
{
    int i;
    for (i=0; i<1000; i++) {
        alloc_one_meg();
    }
}

解决方案

Allocated memory can be in two states in Windows: Reserved, and Commited (see the documentation of VirtualAlloc about MEM_RESERVE: "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.").

If you allocate memory but do not use it, it remains in the Reserved state, and the OS doesn't count that as used memory. When you try to use it (whether it is only on write, or on both read and write, I do not know, you might want to do a test to find out), it turns into Commited memory, and the OS counts it as used.

Also, the memory allocated by malloc will not be full of 0's (actually it may happen to be, but it's not guaranteed), because you have not initialised it.

这篇关于分配不使用的内存,除非初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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