2 ms·
That is incorrect. If I call std::vector<int>::reserve(1<<30) on Linux w/ GNU standard library, using the GNU default allocator, memory is lazily allocated:
by jeffbee 5y ago
That is incorrect. If I call std::vector<int>::reserve(1<<30) on Linux w/ GNU standard library, using the GNU default allocator, memory is lazily allocated:
$ ./1
allocating 4294967296 bytes
^Z
[1]+ Stopped ./1
$ grep -E VmSize\|RssAnon /proc/$!/status
VmSize: 4199804 kB
RssAnon: 144 kB
- _4r6j 5y agoYeah i verified the same thing on Linux. I will update the README with that.
- edflsafoiewq 5y agoWhether this works depends on several things, like /proc/sys/vm/overcommit_memory and how much RAM+swap you actually have available.
- jeffbee 5y agoNo, this is the behavior for any value of vm.overcommit_memory.
- edflsafoiewq 5y ago~ $ 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 ~ $
- jeffbee 5y agoI see, thanks. It didn't seem to matter on my box, but perhaps that's what "heuristic overcommit" means: outcomes may vary?