2017-03-01 18:32:21 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const log = require('./log.js');
|
2017-08-24 02:51:02 -05:00
|
|
|
const logLevels = require('./enums/logLevels.js');
|
2018-04-11 08:06:46 -05:00
|
|
|
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);
|
|
|
|
|
2017-08-24 05:48:32 -05:00
|
|
|
/**
|
|
|
|
* Gathers system memory and logs it to the remote system
|
|
|
|
*/
|
2017-03-01 18:32:21 -06:00
|
|
|
function gatherMemory() {
|
2017-08-24 02:51:02 -05:00
|
|
|
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);
|
|
|
|
}
|
2018-04-11 08:06:46 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
};
|