Home:ALL Converter>How to tell that a v8 isolate instance uses too much memory?

How to tell that a v8 isolate instance uses too much memory?

Ask Time:2016-09-22T05:04:15         Author:markt1964

Json Formatter

I am using Google's v8 engine for embedding javascript in my application. At certain times, I will be invoking user-supplied code, and I want to ensure that it does not behave badly by allocating too much memory. Currently, when the javascript tries to make or resize an array to be too large, for example, I get an unceremonious message:

#
# Fatal error in CALL_AND_RETRY_LAST
# Allocation failed - process out of memory
#

And then entire process crashes with a SIGILL. Obviously, this is not acceptable. I require the ability to run user-supplied code, however... and it is not feasible to manually vet all code before it is executed by the engine.

What I would ideally like to do in this case is simply terminate the isolate that was consuming too much memory (without affecting any other isolates that may be running). Is there any way to designate a maximum amount of memory that a js program is allowed to use before it fails, and so if it exceeds that limit, instead of crashing the process, the invocation of the Run or Call commands would simply return an error or set some status flag indicating that it was abnormally terminated.

Things I have tried so far:

Setting a custom array_buffer allocator when the isolate is created, which tracks how much memory is being used and terminates the isolate when the memory usage gets too high> The Allocate function of my allocator does not ever get called.

Calling AddMemoryAllocationCallback with a function that tracks memory usage and tries to terminate the isolate via TerminateExecution() when the allocations exceeds a certain amount. This function does get called, but I get an out of memory error after this function only has reported a couple of megabytes being used, while I know for a fact that the data being created by the bad behaving v8 function is FAR larger than that.

Setting a fatal error handler via SetFatalErrorHandler and trying to invoke TerminateExecution there. This function does get called, but it does not prevent the process from crashing.

Is there anything else I can try?

Author:markt1964,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39626567/how-to-tell-that-a-v8-isolate-instance-uses-too-much-memory
yy