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?
Community
  • 1
  • 1
Loufs
  • 1,596
  • 1
  • 14
  • 22
  • 1
    You can't really catch it because that in return creates a new error object, which then can't get allocated – Bálint Jan 20 '17 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. – Loufs Jan 20 '17 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. – Bálint Jan 20 '17 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. – Loufs Jan 20 '17 at 19:35
  • @Lofus - There are no "discussion questions" on stack overflow. This is a question/answer site, not a discussion site. Any attempt to post a "discussion question" would be considered off-topic here. I think the only answer to your question: "Can an allocation fail and code operation continue in javascript?" is NO. You simply can't do that in Javascript. Even running lines of code or calling a function requires memory as even function scope variables are object allocations. The usual solution to low memory issues is to change your design so it uses a lot less memory. – jfriend00 Jan 20 '17 at 19:36
  • Again, didn't mean that literally. Does every line really allocate even if it's just an assignment change? Could you post an answer because I am confused how this isn't a possibility. – Loufs Jan 20 '17 at 19:49
  • 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. – Michael Chaney Jan 20 '17 at 19:55

0 Answers0