Skip to content

Instantly share code, notes, and snippets.

@lqqyt2423
Created February 25, 2019 10:20
Show Gist options
  • Save lqqyt2423/87f0b332efa107ade10302da74220044 to your computer and use it in GitHub Desktop.
Save lqqyt2423/87f0b332efa107ade10302da74220044 to your computer and use it in GitHub Desktop.
node 内存展示
'use strict';
const os = require('os');
function format (bytes) {
return (bytes / 1024 / 1024).toFixed(2) + ' MB';
}
function showMem () {
const mem = process.memoryUsage();
console.log('Process: heapTotal ' + format(mem.heapTotal) +
' heapUsed ' + format(mem.heapUsed) +
' rss ' + format(mem.rss) +
' external ' + format(mem.external));
console.log('-------------------------------------------------------------------');
}
function useMem () {
// array
// const size = 20 * 1024 * 1024;
// const arr = new Array(size);
// for (let i = 0; i < size; i++) {
// arr[i] = 0;
// }
// return arr;
// buffer
const size = 200 * 1024 * 1024;
const buffer = new Buffer(size);
for (let i = 0; i < size; i++) {
buffer[i] = 0;
}
return buffer;
}
function showOsMem () {
const total = os.totalmem();
const free = os.freemem();
console.log('OS: total ' + format(total) + ' free ' + format(free));
}
const total = [];
for (let i = 0; i < 15; i++) {
showOsMem();
showMem();
total.push(useMem());
}
showMem();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment