SymphonyElectron/js/memoryMonitor.js

60 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-03-01 18:32:21 -06:00
'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;
2017-03-01 18:32:21 -06:00
// once a minute
setInterval(gatherMemory, 1000 * 60);
/**
* Gathers system memory and logs it to the remote system
*/
2017-03-01 18:32:21 -06:00
function gatherMemory() {
let memory = process.getProcessMemoryInfo();
let details =
2017-03-01 18:32:21 -06:00
'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
};