简体   繁体   中英

C++: Request more than 2GB of memory

I have the following code:

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

typedef uint64_t     Huge_num;  // 8 bytes
typedef Huge_num *   Huge_Arr; // Array of 8 byte elements

using namespace std;

// Function: Return a huge array
static Huge_Arr* requestMem(int n)
{
   cout << "requesting :" 
        << (sizeof(Huge_num)*n)/(1024*1024*1024) 
        << "GB" << endl;

   Huge_Arr *buff;
   try {
      buff = new Huge_Arr[n];
   }
   catch (bad_alloc){
      cout << "Not enough mem!" << endl; 
      exit(-1);
   }
   return buff;
}

// Main
int main(){
    for (int i=1; i< 10; i++){
         Huge_Arr *mem = requestMem(i*90000000);
         delete mem;
    }
}

For some reason malloc is only able to grab no more than 2GB of memory before throwing bad_alloc() error.

I understand that on 32-bit systems, the maximum address space is indeed on same order of 2GB.

But I am using Ubuntu 12.04 x86_64, which should be able to handle larger memory requests, right?

EDIT: Turns out the answer was I was compiling with g++ -std=c00x , which is 32-bit? Not sure, either way I changed the Makefile to remove the -std flag and it compiled fine

malloc is allocating contiguous memory. The fact that you get a badalloc requesting 2 GB of memory doesn't necessarily mean that you have less than 2 GB of memory available, it can also mean that the memory available is fragmented. Try allocating smaller chunks if you want to test how much memory is available to be allocated on your system.

If you have increase your swap space size, it's possible to have more space for allocating, when RAM does not have enough memory, operating system grows heap space via swapping with disk space , so for this you might make changes in swap file size, this link explain it for best:

All about Linux swap space

Other best way is to turn overcommitting value on , if the overcommitting value sets to off , you can not allocate all of your memory on your system, with turn overcommitting value on you can allocate all of your memory space for your array.

for turn overcommitting value to on :

set /proc/sys/vm/overcommit_memory to 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM