V8 crash if it can't allocate memory? Does it ruin the whole process?

This question is similar to the one I recently asked about LLVM .

V8 allocates JavaScript objects in a manually managed heap, whose memory is ultimately obtained from mmap / VirtualAlloc (on Linux / Windows). However, for its internal data structures, V8 uses standard C ++ containers, such as std::vector . If these containers should allocate memory but cannot, they usually throw std::bad_alloc .

However, V8 is compiled with -fno-exceptions . If exceptions cannot be used, how does the V8 handle a situation where internal data structures cannot allocate memory? Is it a crash? If so, does this remove any process that embeds V8?

+8
c ++ javascript exception stl v8
source share
1 answer

In general, even if your code is compiled with -fno-exceptions (then new will not throw std::bad_alloc , but it will return nullptr ), the standard C ++ libstdC ++ library is compiled with exceptions, then new will still throw std::bad_alloc when you run out of memory.

However, seriously, when you are running out of memory, you can do it best as quickly as possible (most often). If you need some kind of reliability, it’s much easier to have a monitoring process that will restart your application.

What does the V8 do? Obviously, they overloaded the new operator, and when the distribution fails ( malloc() still returns NULL , of course), they call a special function to handle low-memory conditions. It discards some debugging information, reports this error (you may have your own error handler), and then (if the error handler returns) calls FATAL() to exit the application.

Check out the source code for api.cc on GitHub . From the code, simply:

When V8 cannot allocate allocated memory, FatalProcessOutOfMemory is called. The default OOM error handler is called and execution stops.

+5
source share

All Articles