0

I have a lot of "not very well" written JavaScript scripts I run in nodejs environment. It contains memory leaks, infinite loops and whatever code can "regular" (non-programmers) user produce.

What I found out when randomly analyzing execution of those "scripts" was, that some of them has huge rss memory area lets say around 1.0GB while heapTotal might be "just" around 450MB.

Despite reading blog posts about memory layout in nodejs I am not able to explain/simulate such "leak". I tried to create heapdump but obviously I wont find what is stored in the "stack area" because I did not dump that zone.

Do anyone know what has to happen in the source code so we leak out all memory while heap size is much smaller i.e. what would "evil source code" look like to eat space out of heap?

EDIT:

I found out that its pretty simple: const c = Buffer.alloc(1024*1024*1024, 1) consumes 1GB outside of heap. New question arise: How can one "clean out" this space and free the memory up? How can I detect leaky buffers? Is restart only way?

svobol13
  • 1,842
  • 3
  • 25
  • 40
  • In general, the heap has all the "permanent" stuff. In the sense that it's not a local variable that goes away after the function execution. It's hard to say what "evil" code would look but *sort of* `var global = "this is in heap"; function fn() { var temp = "this will disappear after fn() finishes"; }` - you would have a lot of the `global` variety. You might have objects that refer to other objects and [implied globals](https://stackoverflow.com/questions/4909578/what-are-some-of-the-problems-of-implied-global-variables): `function fn() { impliedGlobal = "this never goes away"}`. – VLAZ Oct 07 '19 at 14:45
  • Maybe I miss something but in your example `temp` (string object pointer) is stored in stack i.e. couple of bytes is "outsideheap". The string itself (~40 chars ~80 bytes?) is stored in heap and will go away not sooner than during next GC. The temp string pointer will go away immediately after function finishes (is this true statement?). The example does not leak "out of heap" so the ratio (rss-heap) vs heap is kept low. – svobol13 Oct 07 '19 at 14:56

0 Answers0