Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function startMeasureMemory (subject, simple = true) {
if (ignoreSubject.includes(subject)) {
return () => {}
}
console.time(subject)
const v8 = require('v8')
const before = v8.getHeapStatistics()
console.time(subject)
return function () {
const after = v8.getHeapStatistics()
const diff = {}
for (let key of Object.keys(before)) {
diff[key] = after[key] - before[key]
}
console.info(`= ${subject}`)
if (simple) {
console.timeEnd(subject)
// console.log("diff.used_heap_size", toMB(diff.used_heap_size))
} else {
const table = [before, after, diff]
for (let result of table) {
for (const key in result) {
this.timer = setInterval(() => {
const stats = v8.getHeapStatistics()
// update each metrics that we declared
for (let metricName in this.metricsDefinitions) {
if (typeof stats[metricName] !== 'number') continue
const gauge = this.metricStore.get(metricName)
if (gauge === undefined) continue
gauge.set(this.formatMiBytes(stats[metricName]))
}
// manually compute the heap usage
const usage = (stats.used_heap_size / stats.total_heap_size * 100).toFixed(2)
const usageMetric = this.metricStore.get('heap_used_percent')
if (usageMetric !== undefined) {
usageMetric.set(parseFloat(usage))
}
}, this.TIME_INTERVAL)
'use strict';
require('../common');
const assert = require('assert');
const v8 = require('v8');
const s = v8.getHeapStatistics();
const keys = [
'does_zap_garbage',
'heap_size_limit',
'malloced_memory',
'number_of_detached_contexts',
'number_of_native_contexts',
'peak_malloced_memory',
'total_available_size',
'total_heap_size',
'total_heap_size_executable',
'total_physical_size',
'used_heap_size'];
assert.deepStrictEqual(Object.keys(s).sort(), keys);
keys.forEach(function(key) {
assert.strictEqual(typeof s[key], 'number');
});
get status () {
return {
uptime: os.uptime(),
load: os.loadavg(),
ram: {
total: os.totalmem(),
free: os.freemem(),
usage: process.memoryUsage()
},
cpu: os.cpus(),
v8: V8.getHeapStatistics()
}
}
update() {
this.heap_snapshot.heap_size = v8.getHeapStatistics().used_heap_size;
}
check() {
const mem = v8.getHeapStatistics();
const limit = mem.heap_size_limit;
const total = mem.total_heap_size;
const mult = 1000000;
const limitHR = Math.round(limit / mult * 100) / 100;
const totalHR = Math.round(total / mult * 100) / 100;
const nums = `${total}/${limit}B (${totalHR}/${limitHR}MB)`;
const thresh =
`${limit-this._threshold-total} from threshold (${this._threshold})`;
this.debug(`Heap Snapshot: ${nums} ${thresh}`);
if (total >= limit - this._threshold) {
this.suicide();
return;
setInterval(() => {
log.info("Heap statistics : %j", v8.getHeapStatistics());
}, 60000);
}).catch((e) => {log.error("Failed to listen ", e)})
return Utils.file.stats ( filePath ).then ( stats => {
if ( stats.size < 1000000000 && stats.size < ( require ( 'v8' ).getHeapStatistics ().heap_size_limit * .75 ) ) return;
throw new Error ( 'File too large, try splitting it into smaller ones' );
});