Malloc allocates memory more than RAM

后端 未结 4 463
执念已碎
执念已碎 2020-12-29 15:44

I just executed a program that mallocs 13 MB in a 12 MB machine (QEMU Emulated!) . Not just that, i even browsed through the memory and filled junk in it...



        
4条回答
  •  星月不相逢
    2020-12-29 16:07

    Unless the virtualized OS has swap available, what you're encountering is called overcommit, and it basically exists because the easy way to manage resources in a system with virtual memory and demand/copy-on-write pages is not to manage them. Overcommit is a lot like a bank loaning out more money than it actually has -- it seems to work for a while, then things come crashing down. The first thing you should do when setting up a Linux system is fix this with the command:

    echo "2" > /proc/sys/vm/overcommit_memory
    

    That just affects the currently running kernel; you can make it permanent by adding a line to /etc/sysctl.conf:

    vm.overcommit_memory=2
    

提交回复
热议问题