That will actually allocate 4GB to your process, EDIT: OS dependent. This just reserves 4GB of virtual memory addresses.
Is this too simplistic and this package is doing something else?
It's not obvious to me that it buys you much to reserve without committing. You're taking the hint, then ignoring it. Why bother? Why not just do nothing?r
$ ./1
allocating 4294967296 bytes
^Z
[1]+ Stopped ./1
$ grep -E VmSize\|RssAnon /proc/$!/status
VmSize: 4199804 kB
RssAnon: 144 kB ~ $ cat a.cpp
#include <vector>
int main() {
std::vector<int> v;
v.reserve(1ULL<<32);
}
~ $ g++ a.cpp
~ $ sudo sh -c 'echo 0 > /proc/sys/vm/overcommit_memory'
~ $ ./a.out
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
~ $ sudo sh -c 'echo 1 > /proc/sys/vm/overcommit_memory'
~ $ ./a.out
~ $