title: How to tell that a v8 isolate instance uses too much memory?
body: <p>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:</p>
  
  <pre><code>#
  # Fatal error in CALL_AND_RETRY_LAST
  # Allocation failed - process out of memory
  #
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Things I have tried so far:</p>
  
  <p>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.</p>
  
  <p>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 <em>FAR</em> larger than that.</p>
  
  <p>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.</p>
  
  <p>Is there anything else I can try?</p>