Created
February 25, 2019 10:20
-
-
Save lqqyt2423/87f0b332efa107ade10302da74220044 to your computer and use it in GitHub Desktop.
node 内存展示
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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