Is it normal for Node.js' RSS (Resident Set Size) to grow with each request, until reaching some cap?

前端 未结 1 1080
慢半拍i
慢半拍i 2021-02-20 02:27

I\'ve noticed that RSS (Resident Set Size) of my node.js app is growing over time, and considering I\'m having a \"JS Object Allocation Failed - Out of Memory\" error on my serv

1条回答
  •  暖寄归人
    2021-02-20 03:19

    This question is quite old already and yet has no answer, so I'll throw in mine, which references a blog post from 2013-2014 by Jay Conrod who has "worked on optimizing the V8 JavaScript engine for mobile phones".

    V8 tries to be efficient when collecting garbage and for that it uses Incremental marking and lazy sweeping.

    Basically incremental marking is responsible for tracking whether your objects can be collected.

    Incremental marking begins when the heap reaches a certain threshold size.

    Lazy sweeping is responsible for collecting the objects marked as garbage during incremental marking and performing other time consuming tasks.

    Once incremental marking is complete, lazy sweeping begins. All objects have been marked live or dead, and the heap knows exactly how much memory memory could be freed by sweeping. All this memory doesn't necessarily have to be freed up right away though, and delaying the sweeping won't really hurt anything. So rather than sweeping all pages at the same time, the garbage collector sweeps pages on an as-needed basis until all pages have been swept. At that point, the garbage collection cycle is complete, and incremental marking is free to start again.

    I think this explains why your server allocates so much memory until it reaches a certain cap. For a better understanding I recommend reading Jay Conrod's blog post "A tour of V8: Garbage Collection".

    0 讨论(0)
提交回复
热议问题