EventEmitter in JavaScript can cause out of memory
RajaaAbdallah
Posted on June 18, 2023
How can I avoid memory leaks caused by event listeners and event emissions in my Electron app, specifically when continuously reading data from a connected device through a serial port?
I have developed a desktop app using Electron.js and TypeScript. The purpose of this app is to read real-time data from a device connected to it. The app needs to continuously read both analog and digital data from the device. Initially, when the device is connected and data reading is started by listening to the serial port session, everything works fine. However, after approximately 2 hours of continuous data reading from the serial port session, an error occurs:
[electron] <--- Last few GCs --->
[electron]
[electron] [11247:0x3aa8002c0000] 4812376 ms: Scavenge 10.4 (20.9) -> 4.2 (20.9) MB, 0.13 / 0.00 ms (average mu = 1.000, current mu = 1.000) task;
[electron] [11247:0x3aa8002c0000] 4812823 ms: Scavenge 10.4 (20.9) -> 4.2 (20.9) MB, 0.13 / 0.00 ms (average mu = 1.000, current mu = 1.000) task;
[electron] [11247:0x3aa8002c0000] 4813281 ms: Scavenge 10.4 (20.9) -> 4.2 (20.9) MB, 0.19 / 0.00 ms (average mu = 1.000, current mu = 1.000) task;
[electron]
[electron]
[electron] <--- JS stacktrace --->
[electron]
[electron] FATAL ERROR: Cannot grow ExternalPointerTable past its maximum capacity Allocation failed - process out of memory
After initiating the app to continuously read analog data from the device, it functions smoothly for approximately 2 hours. However, after this period, the app crashes due to insufficient memory, resulting in the exception shown in the following image: Image description
so how we can avoid memory leak that occur by event listener and emit the event as i guess in my case?
Posted on June 18, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024