Skip to content

Commit 9640827

Browse files
authored
fix(memcheck): include free swap space in memory check (#1209)
closes #1185 - add swapfree to available to get estimated memory availability
1 parent 48b186b commit 9640827

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

lib/commands/doctor/checks/check-memory.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
'use strict';
21
const sysinfo = require('systeminformation');
3-
const SystemError = require('../../../errors').SystemError;
2+
const {SystemError} = require('../../../errors');
43

54
const MB_IN_BYTES = 1048576;
65
const MIN_MEMORY = 150;
76

8-
function checkMemory() {
9-
return sysinfo.mem().then((memoryInfo) => {
10-
const availableMemory = memoryInfo.available / MB_IN_BYTES;
7+
async function checkMemory() {
8+
const {available, swapfree} = await sysinfo.mem();
9+
const availableMemory = (available + swapfree) / MB_IN_BYTES;
1110

12-
if (availableMemory < MIN_MEMORY) {
13-
return Promise.reject(new SystemError(`You are recommended to have at least ${MIN_MEMORY} MB of memory available for smooth operation. It looks like you have ~${parseInt(availableMemory)} MB available.`));
14-
}
15-
return Promise.resolve();
16-
});
11+
if (availableMemory < MIN_MEMORY) {
12+
throw new SystemError(`You are recommended to have at least ${MIN_MEMORY} MB of memory available for smooth operation. It looks like you have ~${availableMemory} MB available.`);
13+
}
1714
}
1815

1916
module.exports = {

test/unit/commands/doctor/checks/check-memory-spec.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'use strict';
21
const expect = require('chai').expect;
32
const sinon = require('sinon');
43

@@ -44,7 +43,7 @@ describe('Unit: Doctor Checks > Memory', function () {
4443
});
4544

4645
it('fails if not enough memory is available', function () {
47-
const memStub = sinon.stub(sysinfo, 'mem').resolves({available: 10});
46+
const memStub = sinon.stub(sysinfo, 'mem').resolves({available: 10, swapfree: 0});
4847
const memCheck = require(modulePath);
4948

5049
return memCheck.task().catch((error) => {
@@ -55,7 +54,16 @@ describe('Unit: Doctor Checks > Memory', function () {
5554
});
5655

5756
it('passes if there is enough memory', function () {
58-
const memStub = sinon.stub(sysinfo, 'mem').resolves({available: 157286400});
57+
const memStub = sinon.stub(sysinfo, 'mem').resolves({available: 157286400, swapfree: 0});
58+
const memCheck = require(modulePath);
59+
60+
return memCheck.task().then(() => {
61+
expect(memStub.calledOnce).to.be.true;
62+
});
63+
});
64+
65+
it('passes if there is enough memory (using swap space)', function () {
66+
const memStub = sinon.stub(sysinfo, 'mem').resolves({available: 10, swapfree: 157286400});
5967
const memCheck = require(modulePath);
6068

6169
return memCheck.task().then(() => {

0 commit comments

Comments
 (0)