Repository navigation
Description
Preflight Checklist
- I have read the Contributing Guidelines for this project.
- I agree to follow the Code of Conduct that this project adheres to.
- I have searched the issue tracker for an issue that matches the one I want to file, without success.
Issue Details
- Electron Version:
- 11.1.0 and 9.3.5
- Operating System:
- Windows 10 2004
- Last Known Working Electron version:
- NA
Expected Behavior
use of webContents.send + ipcRenderer.on cleans up memory after use
Actual Behavior
In a real-world case, I have a renderer for the UI and a separate browserWindow for an audio process. The audio process monitors audio devices and reports the current volume of all devices via ipcRenderer.send (through preload contextBridge to the main process). The main process then sends this volume data over to the main UI renderer via webContents.send whereas the UI renderer listens via ipcRenderer.on (via preload contextBridge). This happens about once every 100 milliseconds. Memory use of the electron app, especially the renderer, slowly climbs to theoretical Infinitum. The starting memory is about 350MB. Within about a day, it's up to 1,500MB.
It does not matter what kind of content is passed through as arguments, nor its size (garbage collector seems to work on arguments). What matters is how often / frequently webContents.send is called and if there is an ipcRenderer.on listener for it (memory leak still exists even if there is nothing in the listener function so long as the listener exists).
To Reproduce
Electron fiddle with a very simple example calling webContents.send every millisecond (to make the test quick... but the issue exists if you do every 100 milliseconds too but is much slower) and sending a Math.random() random number to the renderer. Memory use will increase over the course of a few minutes. I tested with a 10,000-item map as an argument as well, which made memory spike very quickly and get garbage-collected repeatedly, resulting in an average memory growth of about the same as Math.random(). This leads me to believe the problem is not the data being sent but the IPC execution itself.
Additional Information
Additional info: Using handle/invoke seems to bypass this memory leak. However this is not ideal because renderer UI has to query main every 100 milliseconds for audio volume, and main has to locally store volume information (via another invoke / handle between main and the audio process). This creates additional VU meter lag.
Activity
nornagon commentedon Dec 16, 2020
Hm, I can't reproduce this on macOS. Memory usage is hovering steadily around 130–140mb.
Slightly modified gist including memory logging: https://gist.github.com/a2a70e7f6ff7cd46e0a09485d838ea44
PDStig commentedon Dec 17, 2020
@nornagon I used the fiddle you have. process.memoryUsage is not catching the leak as it seems to be happening in the renderer process.
nornagon commentedon Dec 17, 2020
@Lovinity ah, thanks for the clarification. I see the leak now. It seems to be related to sending the IPC event over
contextBridge.Here's an updated gist with a commented-out modification in preload.js which, if swapped in, results in no leakage. https://gist.github.com/1c4bffaf558fbec5bf7b6de98009968b
cc @MarshallOfSound as this is contextBridge-related.
[-]webContents.send and ipcRenderer.on potential memory leak[/-][+]Memory leak when passing IPC events over contextBridge[/+]nornagon commentedon Dec 17, 2020
(As a side note, btw, exposing
ipcRenderer.ondirectly over the context bridge is a security issue and somewhat defeats the purpose of context isolation. I'd recommend not doing that in any production app.)PDStig commentedon Dec 17, 2020
@nornagon What would you recommend a good alternative be for utilizing ipcRenderer on contextBridge? The only "direct" reference I have is for ipcRenderer.on (but it is still a function calling ipcRenderer.on, not a direct exposure of ipcRenderer.on). For all my "send"s, I use properties on contextBridge that call specific channels on ipcRenderer.send.
nornagon commentedon Dec 17, 2020
@Lovinity expose specific methods that your app needs, instead of allowing the app to listen to any IPC message.
nornagon commentedon Feb 4, 2021
I've updated the gist at https://gist.github.com/1c4bffaf558fbec5bf7b6de98009968b to simplify it somewhat. It now doesn't need any interaction with IPC or events. This is enough to trigger the leak:
Just sending a function back over the bridge results in leaks.
fix: clean up base::LinkedList in context_bridge::ObjectCache
fix: clean up base::LinkedList in context_bridge::ObjectCache (#27630)
fix: clean up base::LinkedList in context_bridge::ObjectCache
fix: clean up base::LinkedList in context_bridge::ObjectCache
fix: clean up base::LinkedList in context_bridge::ObjectCache
fix: clean up base::LinkedList in context_bridge::ObjectCache (#27636)
fix: clean up base::LinkedList in context_bridge::ObjectCache (#27637)
fix: clean up base::LinkedList in context_bridge::ObjectCache (#27638)
fix: add frame-dropping gate to prevent IPC queue leak
fix: renderer memory leak from unbounded Image creation in Streamer (#…