Skip to content

Instantly share code, notes, and snippets.

@Wizek
Forked from Eomm/stress-memory.js
Last active August 19, 2020 14:23
Show Gist options
  • Save Wizek/6dac1858dd601f96cd30095ffd6eeabd to your computer and use it in GitHub Desktop.
Save Wizek/6dac1858dd601f96cd30095ffd6eeabd to your computer and use it in GitHub Desktop.
Check when your server goes out of memory
/**
node --max-old-space-size=4048 --initial-old-space-size=4048 --max-heap-size=4048 stress-memory-1.js
x64
rss 7.62 GB
heapTotal 3.74 GB
heapUsed 3.73 GB
*/
const os = require('os')
console.log(os.arch())
Array(1e8).fill('1')
const mem = process.memoryUsage()
console.log('rss ' + human(mem.rss))
console.log('heapTotal ' + human(mem.heapTotal))
console.log('heapUsed ' + human(mem.heapUsed))
function human (size) {
var i = Math.floor(Math.log(size) / Math.log(1024))
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]
}
/**
node --max-old-space-size=40480 --initial-old-space-size=40480 --max-heap-size=40480 stress-memory-2.js
x64
rss 8.38 GB
heapTotal 4.86 GB
heapUsed 4.86 GB
*/
const os = require('os')
console.log(os.arch())
Array(1e8 + 1e7).fill('1')
const mem = process.memoryUsage()
console.log('rss ' + human(mem.rss))
console.log('heapTotal ' + human(mem.heapTotal))
console.log('heapUsed ' + human(mem.heapUsed))
function human (size) {
var i = Math.floor(Math.log(size) / Math.log(1024))
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment