Malloc内存问题 [英] Malloc Memory Questions

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

问题描述

首先我注意到,当我malloc内存与calloc的内存占用是不同的。我正在处理几个GB的数据集。这个数据是随机的。



我希望我可以malloc大量的内存,并读取任何随机数据转换为浮点数。但是,在进程查看器中查看内存占用情况时,显然不会声明内存(相比calloc,我在这里看到的是较大的脚印)。我运行一个循环将数据写入内存,然后我看到内存占用率攀升。 我是否正确地说,直到我初始化内存才被实际声明?



最后,我通过了1024 * 1024 * 128字节(在进程查看器中为1024 MB)我开始出现段错误。然而,Calloc似乎初始化了最高达1 GB的全部数量。 为什么我在使用malloc在for循环中初始化内存时出现段错误,数目为128MB,为什么内存占用量显示为1024MB? strong>如果从内存中获取大量malloc,然后从中读取,我得到了什么(因为进程查看器显示几乎没有足迹,直到我初始化它)



最后有什么方法可以让我分配4GB以上的内存?我正在测试内存层次结构性能。



#2代码: / p>

  long long int i; 
long long int * test =(long long int *)malloc(1024 * 1024 * 1024);
for(i = 0; i <1024 * 1024 * 128; i ++)
test [i] = i;

sleep(15);


解决方案

1-如果您正在使用32位机器上不能有超过2GB的变量分配给它。



2-如果你在一台64位机器上工作,你可以像RAM一样分配内存+总共交换内存,但是,为一个变量分配所有内存需要很大的内存空间,可能无法使用。尝试使用链接列表,其中每个元素只有1 MB分配,并且您可以获得总共分配的更高内存。



3-如您和Sharth所述,除非你使用你的内存,否则linux不会分配它。


First of all I noticed when I malloc memory vs. calloc the memory footprint is different. I am working with datasets of several GB. It is ok for this data to be random.

I expected that I could just malloc a large amount of memory and read whatever random data was in it cast to a float. However, looking at the memory footprint in the process viewer the memory is obviously not being claimed (vs. calloc where I see a large foot print). I ran a loop to write data into the memory and then I saw the memory footprint climb. Am I correct in saying that the memory isn't actually claimed until I initialize it?

Finally after I passed 1024*1024*128 bytes (1024 MB in the process viewer) I started getting segfaults. Calloc however seems to initialize the full amount up to 1 GB. Why do I get segfaults when initializing memory in a for loop with malloc at this number 128MB and why does the memory footprint show 1024MB?

If malloc a large amount from memory and then read from it what am I getting (since the process viewer shows almost no footprint until I initialize it)?

Finally is there any way for me to alloc more than 4GB? I am testing memory hierarchy performance.

Code for #2:

    long long int i;
    long long int *test=(long long int*)malloc(1024*1024*1024);
    for (i=0;i<1024*1024*128;i++)
            test[i]=i;

    sleep(15);

解决方案

1- If you are working on a 32-bit machine you can't have a variable with more than 2GBs allocated to it.

2- If you are working on a 64-bit machine you can allocate as much as RAM+Swap memory in total, however, allocating all for one variable requires a big consequent chunk of memory which might not be available. Try it with a linked list, where each element has only 1 MB assigned and you can achieve a higher memory allocated in total.

3- As noted by you and Sharth, unless you use your memory, linux won't allocate it.

这篇关于Malloc内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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