简体   繁体   中英

C++ stdlib.h system() command wont work after allocating 8GB memory

Im running the following sample code under ubuntu 13.04 64bit having 16 GB mem installed using intel compiler 14 toolchain.

Im having an issue regarding the system("do something bashy..") wont work after my application allocting at a certein point. After fighting the problem for a few hours i came to realise that the fact i allocate 8GB worth of memory at that certain point does not allow me to use system() command any further.

Needless to say, that befor i allocate that memory, i can use system freely.

Code snippet:

#include <string.h>
#include <iostream>
#include "stdlib.h"

int main() {
char ** buffer = new char*[100];

system("logger TRYING..!");
for(int i= 1; i<=80; i++)
{
   buffer[i] = new char[200*1000*1000];
}

system("logger SUCCESS..!");

return 0;
}

Thanks for the help..!

system calls fork , which will essentially double the needed RAM from your process. Since you're using 8 GB and only have 16 GB, you don't have enough, and the fork fails. That said, fork is implemented with copy-on-write pages, meaning that if you don't alter the memory of the child process, the RAM isn't actually duplicated. In this case, you wont be writing to that memory, but the OS doesn't know that, so the fork fails. You should confirm this by checking the return value for system (-1 if fork failed).

You might try enabling overcommit memory to allow you to, well, "overcommit memory." This should let the fork succeed. In your case, I'd probably only enable it temporary.

# Enable overcommit
echo 1 > /proc/sys/vm/overcommit_memory

# Disable overcommit
echo 0 > /proc/sys/vm/overcommit_memory

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