How does the Node.js (v8) Garbage Collector work?

1: yes :-)

2: In general the leaking of memory when using event listeners is that the object that is listening is prevented from being garbage collected because the object that is emitting is keeping a reference to it.

So in your code the onSuccess function will be referenced by your request object. However, that onSuccess is only one function that is being reused as a listener for all request objects so that should not lead to memory buildup.

Sidenote: I don't know the innards of redisClient and RequestObject but to me it also looks like the request will be ready for garbage collection as soon as the initRequest function completes, that is possibly before any of its listeners are called.


As far as i can see the request object should only exists within the initRequest function, and should therefor be marked for garbage collection when the function terminates.