5

How can I know when my application is running out of memory.

For me, I'm doing some video transcoding on the server and sometimes it leads to out of memory errors.

So, I wish to know when the application is running out of memory so I can immediately kill the Video Transcoder.

Thank you.

2
  • This will be hard to do precisely. Your best bet is to measure how much memory your process consumes by doing some OS syscalls. Commented Apr 9, 2022 at 18:58
  • Check the used heap memory. How are you transcoding the video? Are you using streams or buffering the input/output? I would suggest ffmpeg when it comes to work with media files. Commented Apr 9, 2022 at 19:16

2 Answers 2

10

Use v8.getHeapStatistics(). It is more informative than process.memoryUsage(). In particular, it includes heap_size_limit which tells you the true limits of your Node.js process's memory.

import * as v8 from 'node:v8';

const maxHeap = v8.getHeapStatistics().heap_size_limit;

// get the percentage of the max heap currently in use
function getPercentOfMaxHeapInUse() {
    return v8.getHeapStatistics().used_heap_size / maxHeap;
}

// error out if over 80% of the heap is in use
if (getPercentOfMaxHeapInUse() > 0.8) {
    throw new Error('using too much memory');
}

You can modify the max heap space with the --max_old_space_size flag, like this:

# set the heap max to 3 GB (1024 * 3)
node --max_old_space_size=3072 index.js
Sign up to request clarification or add additional context in comments.

Comments

4

You can see how much memory is being used with the built-in process module.

const process = require("process");

The process module has a method called memoryUsage, which shows info on the memory usage in Node.js.

console.log(process.memoryUsage());

When you run the code, you should see an object with all of the information needed for memory usage!

$ node index.js
{
  rss: 4935680,
  heapTotal: 1826816,
  heapUsed: 650472,
  external: 49879,
  arrayBuffers: 9386
}

Here is some insight on each property.

  • rss - (Resident Set Size) Amount of space occupied in the main memory device.
  • heapTotal - The total amount of memory in the V8 engine.
  • heapUsed - The amount of memory used by the V8 engine.
  • external - The memory usage of C++ objects bound to JavaScript objects (managed by V8).
  • arrayBuffers - The memory allocated for ArrayBuffers and Buffers.

For your question, you might need to use heapTotal and heapUsed. Depending on the value, you can then shut the service down. For example:

const process = require("process");

const mem = process.memoryUsage();
const MAX_SIZE = 50; // Change the value to what you want

if ((mem.heapUsed / 1000000) >= MAX_SIZE) {
  videoTranscoder.kill(); // Just an example...
}

The division by one million part just converts bytes to megabytes (B to MB).

Change the code to what you like - this is just an example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.