How to estimate the memory used by event listeners in NodeJS

Viewed 48

When there are more than 10 event listeners, NodeJS alerts a warning:

(node:56301) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 11 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit at AbortSignal.[kNewListener] (node:internal/event_target:426:17)

For the application I am working with, it is fine to have more than 10 event listeners at a given point in time, so I will like to increase the threshold at which this event is triggered. But I need a methodical way to determine what to increase it to. Hence I was thinking, is it possible to estimate the memory used per each event listener? If possible, this can help estimate what the new limit should be.

2 Answers

Node does nor provide such strict control of memory usage, but helps you to prevent overusage of memory. Event listeners do not consume too much memory, but they can cause a memory leak. You can remove listener limit at all, but make sure that memory leak will not occur

emitter.setMaxListeners(0)

I would try not to use that much eventListeners, since it consumes more memory

So try to avoid it possible. if you still want it here is the solution.

const { setMaxListeners } = require('events');

const abc = new AbortController()

setMaxListeners(20, abc.signal);

// events module is automatically added to global space, 
// so you can also do this `events.setMaxListeners`, no need to import it.

// ... 
Related