We know node.js provides us with great power but with great power comes great responsibility.
As far as I know the V8 engine doesn't do any garbage collection. So what are the most common mistakes we should avoid to ensure that no memory is leaking from my node server.
EDIT: Sorry for my ignorance, V8 does have a powerful garbage collector.
V8 has a powerful and intelligent garbage collector in build.
Your main problem is not understanding how closures maintain a reference to scope and context of outer functions. This means there are various ways you can create circular references or otherwise create variables that just do not get cleaned up.
This is because your code is ambigious and the compiler can not tell if it is safe to garbage collect it.
A way to force the GC to pick up data is to null your variables.
How does v8 know whether it is safe to garbage collect big object when it's in an event handler? It doesn't so you need to tell it it's no longer used by setting the variable to null.
Various articles to read:
active garbage collection with:
and use with:
Happy Coding :)
I wanted to convince myself of the accepted answer, specifically:
So I wrote the following code to demonstrate how variables can fail to be cleaned up, which people may find of interest.
If you have
watch -n 0.2 'ps -o rss $(pgrep node)'
running in another terminal you can watch the leak occurring. Note how commenting in either thebuffer = null
or usingnextTick
will allow the process to complete: