Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does V8 crash if it cannot allocate memory? Does this crash the entire process?

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?

like image 704
user200783 Avatar asked Aug 02 '16 02:08

user200783


1 Answers

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.

like image 117
Adriano Repetti Avatar answered Oct 14 '22 09:10

Adriano Repetti