SymphonyElectron/js/memoryMonitor.js
Kiran Niranjan 2de86c8003 Electron-136 (Optimize memory by reloading the app when the user is idle) (#335)
- Refresh app when memory exceeds the threshold and if user is inactive
- Add logic to reload app when memory exceeds threshold and if user is inactive
- Add reload threshold
- Fix unit test
2018-04-11 18:36:46 +05:30

60 lines
1.9 KiB
JavaScript

'use strict';
const log = require('./log.js');
const logLevels = require('./enums/logLevels.js');
const { getMainWindow, setIsAutoReload } = require('./windowMgr');
const systemIdleTime = require('@paulcbetts/system-idle-time');
const { getConfigField } = require('./config');
const maxMemory = 800;
let maxIdleTime = 4 * 60 * 1000;
let reloadThreshold = 30 * 60 * 1000;
let reloadedTimeStamp;
// once a minute
setInterval(gatherMemory, 1000 * 60);
/**
* Gathers system memory and logs it to the remote system
*/
function gatherMemory() {
let memory = process.getProcessMemoryInfo();
let details =
'workingSetSize: ' + memory.workingSetSize +
' peakWorkingSetSize: ' + memory.peakWorkingSetSize +
' privatesBytes: ' + memory.privatesBytes +
' sharedBytes: ' + memory.sharedBytes;
log.send(logLevels.INFO, details);
}
/**
* Method that checks memory usage every minute
* and verify if the user in inactive if so it reloads the
* application to free up some memory consumption
*
* @param memoryInfo
*/
function optimizeMemory(memoryInfo) {
const memoryConsumed = (memoryInfo && memoryInfo.workingSetSize / 1024) || 0;
const canReload = (!reloadedTimeStamp || (new Date().getTime() - reloadedTimeStamp) > reloadThreshold);
if (memoryConsumed > maxMemory && systemIdleTime.getIdleTime() > maxIdleTime && canReload) {
getConfigField('memoryRefresh')
.then((enabled) => {
if (enabled) {
const mainWindow = getMainWindow();
if (mainWindow && !mainWindow.isDestroyed()) {
setIsAutoReload(true);
reloadedTimeStamp = new Date().getTime();
log.send(logLevels.INFO, 'Reloading the app to optimize memory usage');
mainWindow.webContents.reload();
}
}
});
}
}
module.exports = {
optimizeMemory
};