c linux new使内存耗尽_如何在64-linux上的c ++中使用new运算符动态分配10 G之类的大内存?...

I need to dynamically allocate a larger float array for a special application using C++ new operator, like 10G. The code running on 64-ubuntu-14.04 Linux OS with 64G memory.

When I set the request of memory as about 7G ,1879048192x4/(1024x1024x1024)=7G (float has 4 bytes), like this :

float * data;

data = new float[1879048192];

The program works well, but when I try to increase the request to 10G , I got a what(): std::bad_alloc. I also try to use malloc() to take place of new operator:

data =(float*)malloc(1879048192*sizeof(float));

But obtain the same result. My ulimit -a is this:

core file size (blocks, -c) 0

data seg size (kbytes, -d) unlimited

scheduling priority (-e) 0

file size (blocks, -f) unlimited

pending signals (-i) 514689

max locked memory (kbytes, -l) 64

max memory size (kbytes, -m) unlimited

open files (-n) 1024

pipe size (512 bytes, -p) 8

POSIX message queues (bytes, -q) 819200

real-time priority (-r) 0

stack size (kbytes, -s) 8192

cpu time (seconds, -t) unlimited

max user processes (-u) 514689

virtual memory (kbytes, -v) unlimited

file locks (-x) unlimited

Someone may say there might be no 10G continual memory for the allocation, but I close all other progresses and the total memory is 64G. I want to know whether I can obtain this larger array or not, and how. Does the Linux limit the max number for this dynamically allocation? Where and how?

解决方案

I don't see a problem when I try it. Both new and malloc worked. My system runs Ubuntu 15.04 and has 16G of ram.

However, if I try to use the memory, I found I needed to be very careful about the types of vars used to index into the data array.

For instance, the program below can do undesirable things with only long intand float, because a long int has a max value of 2^31 and the array length for 10Gi will be longer than 2^31. Also, floats only add 1.0 at a time to 16777216.0. With doubles, it is ok to use long int indexes here because the array is shorter.

use10G.c++

#include

#include

int main(int argc, char **argv){

long long int ten = 10;

long long int megi = 1024*1024;

long long int gigi = 1024*megi;

long long int asize = (ten*gigi)/((long int) sizeof(double));

double * data = new double[asize];

long long int i=2;

printf("A double is %zd bytes\n", (size_t) sizeof(double));

printf("Array size is %lli \n", asize);

data[0]=0.0;

data[1]=1.0;

while (i

data[i]=data[i-1]+1.0;

++i;

}

printf("%lf\n", (double) data[asize-1]);

printf("success\n");

exit(EXIT_SUCCESS);

}

A double is 8 bytes

Array size is 1342177280

1342177279.000000

success

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值