This question is similar to one I recently asked about LLVM.
V8 allocates JavaScript objects on a manually-managed heap, memory for which is ultimately obtained from mmap/VirtualAlloc (on Linux/Windows). However, for its internal data structures, V8 uses C++ standard containers such as std::vector
. If these containers need to allocate memory, but are unable to, they usually throw std::bad_alloc
.
However, V8 is compiled with -fno-exceptions. If exceptions cannot be used, how does V8 handle the situation where the internal data structures are unable to allocate memory? Does it crash? If so, does this take down any process which embeds V8?
In general even if your code is compiled with -fno-exceptions
(then new
won't throw std::bad_alloc
but it will return nullptr
instead) the standard C++ library libstdc++ is compiled with exceptions then new
will still throw std::bad_alloc
when you're running out-of-memory.
That said, seriously, when you're running out-of-memory to crash as fast as possible is (more often than not) the best thing you can do. If you need some some sort of reliability it's much easier to have a monitor process that will restart your application.
What V8 does? Obviously they overloaded new
operator and when allocation fails (malloc()
still returns NULL
, of course) they call a special function to handle low memory conditions. It dumps some debug information, report this error (you may have a custom error handler) and then (if error handler returns) call FATAL()
to quit the application.
Browse source code at api.cc on GitHub. From code, simply:
When V8 cannot allocated memory FatalProcessOutOfMemory is called. The default OOM error handler is called and execution is stopped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With