Electron React possible EventEmitter memory leak detected

One day, when running React page, the front-end console reported the following node error:

warning: possible EventEmitter memory leak detected. 11 request listeners added. Use emitter.setMaxListeners() to increase limit.

This article records the method to solve this problem.

Causes

By default, if there are more than 10 listeners for a specific event, this warning will be output for reminding. This warning is useful for finding memory leaks, but not every event should be limited to 10.

solve the problem

Palliative treatment

call emitter.setMaxListeners(n) Method, which can set the maximum number of specific event listeners.
For React front page, ipcRenderer It is inherited from the instance of Node Event. You can call the object method directly.

 ipcRenderer.setMaxListeners(100);

Cure the root cause

First, confirm whether so many listeners are necessary.
If there are a bunch of subcomponents under a component, what if there are 101 subcomponents if each subcomponent listens to the same event? How about the performance of 100 monitoring? 200 times?
You can consider saving the data in the parent component state and passing it to the child component through props, or consider other methods.

References

  1. [Caption] [Electron] Electron Electron Logo
  2. [Document] [nodejs. org] Nodejs Documentation Event
Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/4922.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*