Skip to content

Instantly share code, notes, and snippets.

@xuwanghu
Created December 7, 2022 05:18
Show Gist options
  • Save xuwanghu/d61b511abf80aa691375cdfd30a8474e to your computer and use it in GitHub Desktop.
Save xuwanghu/d61b511abf80aa691375cdfd30a8474e to your computer and use it in GitHub Desktop.
Whenever a worker accesses a file in the asar archive package for the first time, electron will cache part of the content of the asar archive package. When the worker ends, the cache will not be cleared.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>press the button</title>
</head>
<body>
<button id="newWorker">new worker</button>
<script src="./renderer.js"></script>
</body>
</html>
const {app, BrowserWindow} = require('electron')
const path = require('path')
function createWindow () {
const mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegrationInWorker:true
}
})
mainWindow.loadFile('index.html')
mainWindow.webContents.openDevTools()
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
{
"name": "test_leak_for_worker",
"version": "1.0.0",
"main": "index.js",
"author": "webster.xu",
"license": "MIT",
"devDependencies": {
"electron": "^17.0.0",
"electron-builder": "^23.6.0"
},
"dependencies": {
"uuid": "8.3.0",
"@electron/remote": "2.0.8",
"electron-log": "4.4.8",
"electron-prompt": "1.7.0",
"electron-store": "5.1.0",
"electron-updater": "5.1.0",
"eventemitter2": "6.0.0",
"ext-name": "5.0.0",
"file-type": "^12.4.0",
"glob": "8.0.1",
"inversify": "5.0.1",
"inversify-inject-decorators": "3.1.0",
"lodash": "4.17.11",
"macos-notification-state": "1.3.6",
"mobx": "5.13.0",
"mousetrap": "1.6.3",
"promise-timeout": "1.3.0",
"read-chunk": "^3.2.0",
"reflect-metadata": "0.1.13",
"ts-javascript-state-machine": "3.1.3",
"unused-filename": "2.1.0",
"url-parse": "1.4.4",
"windows-focus-assist": "1.3.0",
"windows-quiet-hours": "2.0.0",
"check-internet-connected": "^2.0.1",
"electron-settings": "3.1.1",
"get-uv-event-loop-napi-h": "^1.0.6",
"minimatch": "^3.0.4",
"mocha": "^6.2.0",
"multiparty": "^4.2.1",
"node-addon-api": "^3.1.0",
"7zip": "^0.0.6",
"xmlbuilder": "^15.1.1"
},
"scripts": {
"start": "electron .",
"pack": "electron-builder --dir",
"dist": "electron-builder"
},
"build": {
"appId": "test_leak_for_worker"
}
}
document.getElementById('newWorker').addEventListener('click', function(){
var worker = new Worker('worker.js')
worker.onmessage = (e) => {
if(e.data == 'workerEnds')
{
console.log('receive ends and clear worker')
worker.terminate()
worker = null
}
}
})
//uuid is a module in asar archive
//here to access the file in asar
const uuid = require('uuid')
console.log("worker start............,pid ", process.pid)
console.log("worker end............")
setTimeout(() => {
postMessage('workerEnds')
}, 2000);
@xuwanghu
Copy link
Author

xuwanghu commented Dec 7, 2022

The purpose of adding so many dependencies is to make the asar file large enough so that the memory leaks are more noticeable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment