Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator new and bad_alloc on linux

On Linux, malloc doesn't necessarily return a null pointer if you're out of memory. You might get back a pointer and then have the OOM killer start eating processes if you're really out of memory. Is the same true for c++'s operator new or will you get the bad_alloc exception?

like image 625
pythonic metaphor Avatar asked Jan 01 '26 18:01

pythonic metaphor


1 Answers

It's a kernel function rather than a language function - and you can control it with the vm.overcommit_memory and vm.overcommit_ratio sysctls.

They're visible in the proc filesystem at /proc/sys/vm/overcommit_memory and /proc/sys/vm/overcommit_ratio.

like image 100
caf Avatar answered Jan 03 '26 07:01

caf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!