0

So, I recently had an overzealous loop that caused this fun print out to happen:

<--- Last few GCs --->

   10472 ms: Scavenge 960.5 (998.3) -> 960.5 (998.3) MB, 0.0 / 0 ms (+ 1.0 ms in 1 steps since last GC) [allocation failure] [incremental marking delaying mark-sweep].
   10641 ms: Mark-sweep 960.5 (998.3) -> 577.9 (615.7) MB, 169.6 / 0 ms (+ 1.0 ms in 2 steps since start of marking, biggest step 1.0 ms) [last resort gc].
   10795 ms: Mark-sweep 577.9 (615.7) -> 577.6 (615.7) MB, 153.3 / 0 ms [last resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================
[Deleted the trace cause who cares]

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

I fixed it and it's a non-issue but I'm actually approaching the limit very closely. I'm populating an array full of data and mostly just taking an average-analysis of the populated data. It really doesn't need to be the complete list so that is why I was able to stop the loop short and have things be okay.

But breaking a loop short is not always ideal. I know we can't really prevent a failed allocation, but it got me thinking. Can an allocation fail and code operation continue in javascript?

Basically, is this scenario possible with the correct setup?

var a = 10;
var b = 2;
var c = OverAllocatingFunction(); // Everything would crash here
a = 5 - b; // Can we get here?
7
  • 1
    You can't really catch it because that in return creates a new error object, which then can't get allocated Commented Jan 20, 2017 at 19:27
  • I dont mean to literally catch it as a standard error. I just want the allocation line to fail, and then the rest of code below try and continue. I updated the title because it may have been confusing. Commented Jan 20, 2017 at 19:32
  • Even if it continued, how do you do anything then? You don't have any memory left, you can allocate stuff, you can't do operations. Commented Jan 20, 2017 at 19:34
  • You could do operations that dont require allocation and maybe cause garbage collection to step in a free some up. You should post an answer as I mean this more as a discussion question. Commented Jan 20, 2017 at 19:35
  • 1
    Typically an interpreter is going to try garbage collection before giving up and saying "that's it, I can't allocate more memory". It's a fatal error in the vast majority of cases because by the time you get to the point that it's crapping your memory is full and you can't really do anything else. Ideally what you would want is a way to check memory while inside your loop (or whatever) and then stop when available memory reaches a preset limit, say 1MB. That way there's still memory available to do [something]. By the time you're out of memory there's nothing left to do. Commented Jan 20, 2017 at 19:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.