-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory.js
60 lines (50 loc) · 1.03 KB
/
memory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright (c) 2016-present, IBM Research
* Licensed under The MIT License [see LICENSE for details]
*/
"use strict";
function compreensiveBytes (bytesCount)
{
if(bytesCount < 1024)
{
return `${bytesCount}b`;
}
else if(bytesCount < 1024 * 1024)
{
return `${ (bytesCount / 1024).toFixed(2)}kb`;
}
else
{
return `${ (bytesCount / (1024 * 1024)).toFixed(2)}mb`;
}
}
function Memory(logger)
{
this.logger = logger || console;
}
Memory.prototype.getCurrentMemory = function (asNumber = false)
{
return asNumber ? process.memoryUsage().heapUsed : compreensiveBytes(process.memoryUsage().heapUsed)
}
Memory.prototype.checkMemory = function (...args)
{
if(global.gc)
{
global.gc();
}
if(process)
{
this.logger.log.apply(this.logger, args.concat([this.getCurrentMemory()]));
}
else
{
logger.log("N/A");
}
}
function checkMemory(...args)
{
let memory = new Memory();
memory.checkMemory(...args);
}
Memory.checkMemory = checkMemory;
module.exports = Memory;