Skip to content

Instantly share code, notes, and snippets.

@gorshkov-leonid
Last active August 19, 2025 11:00
Show Gist options
  • Save gorshkov-leonid/38dfe17e6a35f2ed1603fbede3788869 to your computer and use it in GitHub Desktop.
Save gorshkov-leonid/38dfe17e6a35f2ed1603fbede3788869 to your computer and use it in GitHub Desktop.
node-tricks.md

Check memory:

# NODE_OPTIONS=--max-old-space-size=4096
# node --max-old-space-size=4096 memory.js
node --max-old-space-size=4096 -e "console.log(JSON.stringify(v8.getHeapStatistics()));"
node -e "console.log(JSON.stringify(require('os').freemem()));"
node -e "console.log(JSON.stringify(require('os').totalmem()));"

measure memory

function logMemory() {
    if (typeof process != 'undefined') {
        console.log(`Node: ${process.memoryUsage().heapUsed / Math.pow(1000, 2)} MB`);
    } else if (performance) {
        console.log(`Browser: ${performance.memory.usedJSHeapSize / Math.pow(1000, 2)} MB`);
    } else {
        throw ('Where d-heck are you trying to run me?');
    }
}

function measureMemory() {
    const arraySize = 25 * Math.pow(1000, 2);
    logMemory();
    (function() {
        const array1 = new Array(arraySize).fill(1.1);
    logMemory();
    })();
    (function() {
        const array2 = new Array(arraySize).fill(1);
    logMemory()
    })();
    
    setTimeout(() => {
        logMemory();
    }, 5000);
}

measureMemory();

https://gist.github.com/gorshkov-leonid/fec76ec4b829a1d63b1da7ea3ed255e1?permalink_comment_id=5002758#gistcomment-5002758 https://yonatankra.com/measuring-used-js-heap-size-in-nodejs/

@gorshkov-leonid
Copy link
Author

the same is for chrome:
flags:

--enable-precise-memory-info
--js-flags="--max_old_space_size=8000 --max_semi_space_size=8000"
 console.log((window.performance as any).memory)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment