Add test to verify memory is released after sandboxed popup is closed

This commit is contained in:
Thiago de Arruda 2017-05-01 10:17:15 -03:00
parent bbe21cce67
commit a8640fb8a3
3 changed files with 61 additions and 0 deletions

View file

@ -1150,6 +1150,29 @@ describe('BrowserWindow module', function () {
})
w.loadURL('file://' + path.join(fixtures, 'pages', 'window-open.html'))
})
it('releases memory after popup is closed', (done) => {
w.destroy()
w = new BrowserWindow({
show: false,
webPreferences: {
preload: preload,
sandbox: true
}
})
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?allocate-memory'))
w.webContents.openDevTools({mode: 'detach'})
ipcMain.once('answer', function (event, {bytesBeforeOpen, bytesAfterOpen, bytesAfterClose}) {
const memoryIncreaseByOpen = bytesAfterOpen - bytesBeforeOpen
const memoryDecreaseByClose = bytesAfterOpen - bytesAfterClose
// decreased memory should be less than increased due to factors we
// can't control, but given the amount of memory allocated in the
// fixture, we can reasonably expect decrease to be at least 70% of
// increase
assert(memoryDecreaseByClose > memoryIncreaseByOpen * 0.7)
done()
})
})
})
})

11
spec/fixtures/api/allocate-memory.html vendored Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
window.bigBuffer = new Uint8Array(1024 * 1024 * 64)
window.bigBuffer.fill(5, 50, 1024 * 1024)
</script>
</body>
</html>

View file

@ -1,5 +1,18 @@
<html>
<script type="text/javascript" charset="utf-8">
function timeout(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
async function invokeGc () {
// it seems calling window.gc once does not guarantee garbage will be
// collected, so we repeat 10 times with interval of 100 ms
for (let i = 0; i < 10; i++) {
window.gc()
await timeout(100)
}
}
if (window.opener) {
window.callback = () => {
opener.require('electron').ipcRenderer.send('answer', document.body.innerHTML)
@ -7,6 +20,20 @@
} else {
const {ipcRenderer} = require('electron')
const tests = {
'allocate-memory': async () => {
await invokeGc()
const {privateBytes: bytesBeforeOpen} = process.getProcessMemoryInfo()
let w = open('./allocate-memory.html')
await invokeGc()
const {privateBytes: bytesAfterOpen} = process.getProcessMemoryInfo()
w.close()
w = null
await invokeGc()
const {privateBytes: bytesAfterClose} = process.getProcessMemoryInfo()
ipcRenderer.send('answer', {
bytesBeforeOpen, bytesAfterOpen, bytesAfterClose
})
},
'window-events': () => {
document.title = 'changed'
},