Skip to content

Instantly share code, notes, and snippets.

@fahimbabarpatel
Last active June 28, 2024 10:22
Show Gist options
  • Save fahimbabarpatel/6481faa81b925c400a168b4b8e18ec45 to your computer and use it in GitHub Desktop.
Save fahimbabarpatel/6481faa81b925c400a168b4b8e18ec45 to your computer and use it in GitHub Desktop.
This JS script will help you to know default memory allowed to NodeJS runtime on the basis of your hardware and system OS
  1. save below code in nodejs_default_memory_check.js,
  2. execute it - node nodejs_default_memory_check.js . You can see this script fails and thats default RAM limit of NodeJS. Plz check screenshot in below comment. In Latest NodeJS versions default RAM is 4GB (still on the basis of your hardware and system OS, RAM will differ)
  3. use max-old-space-size option if you want to increase memory limit. It accepts value in MB. In this example we are setting 8GB RAM node --max-old-space-size=8192 nodejs_default_memory_check.js
function allocateMemory(size) {
  // Simulate allocation of bytes
  const numbers = size / 8;
  const arr = [];
  arr.length = numbers;
  for (let i = 0; i < numbers; i++) {
    arr[i] = i;
  }
  return arr;
}



const memoryLeakAllocations = [];

const field = "heapUsed";
const allocationStep = 10000 * 1024; // 10MB

const TIME_INTERVAL_IN_MSEC = 40;

setInterval(() => {
  const allocation = allocateMemory(allocationStep);

  memoryLeakAllocations.push(allocation);

  const mu = process.memoryUsage();
  // # bytes / KB / MB / GB
  const gbNow = mu[field] / 1024 / 1024 / 1024;
  const gbRounded = Math.round(gbNow * 100) / 100;

  console.log(`Heap allocated ${gbRounded} GB`);
}, TIME_INTERVAL_IN_MSEC);
@fahimbabarpatel
Copy link
Author

Screenshot from 2024-06-28 15-39-22

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