opencv4nodejs: Memory Leak: External Memory Growth When Using Async Methods in OpenCV Template Matching

I’ve identified a memory leak in a Node.js project using OpenCV for template matching operations. The external memory usage keeps increasing over time despite explicit release calls.

Environment

  • Node.js version: 20
  • OpenCV version: 4.10
  • OS: macOS

Code Example

const full = await cv.imdecodeAsync(fs.readFileSync('src/dev/full.jpg'));
const img = await cv.imdecodeAsync(fs.readFileSync('src/dev/1.jpg'));
const matched = await full.matchTemplateAsync(img, cv.TM_CCOEFF_NORMED);
const minMax = matched.minMaxLoc();
const { maxVal } = minMax;
const matchPercentage = maxVal * 100;
console.log('matchPercentage', matchPercentage);
full.release();
img.release();
matched.release();

Memory Monitoring

setInterval(() => {
  const used = process.memoryUsage();
  console.log({
    rss: `${Math.round(used.rss / 1024 / 1024 * 100) / 100} MB`,
    heapTotal: `${Math.round(used.heapTotal / 1024 / 1024 * 100) / 100} MB`,
    heapUsed: `${Math.round(used.heapUsed / 1024 / 1024 * 100) / 100} MB`,
    external: `${Math.round(used.external / 1024 / 1024 * 100) / 100} MB`,
  });
}, 1000);

Issue

Despite explicitly calling release() on all OpenCV Mat objects (full, img, matched), the external memory usage continues to grow:

{
  "rss": "1488.89 MB",
  "heapTotal": "96.58 MB",
  "heapUsed": "61.1 MB",
  "external": "4220.46 MB"
}

If you don’t use asynchronous methods, this problem doesn’t exist.

About this issue

Most upvoted comments

I’ve noticed that the handling has changed from using Napi::AsyncWorker to a custom ThreadPool for task execution, relying on Napi::ThreadSafeFunction for communication with the main thread. Additionally, since an unbounded queue is now being used, it might be a good idea to perform stress tests to simulate high concurrency scenarios.

That said, regardless of these recent changes, it seems my issue with unreleased memory is OS-related. After calling release on matrices, I can see that the corresponding objects in Node.js become empty, which confirms that the integration with OpenCV works correctly. However, when monitoring memory usage in Activity Monitor or checking rss with process.memoryUsage(), I observe that memory does not decrease.

I pushed the process to its limit, where memory usage stabilises and does not grow further. On my local setup with an M1 Pro, this limit is around 4 GB. I’ve attached a video demonstrating how memory usage goes above 4 GB but quickly falls back below the threshold.

It seems there’s nothing I can do about this behavior, so I’ll just configure a memory limit in the Kubernetes cluster for my service.

memory.above.threshold.mov

Yes, this issue is related to a specific operating system. My understanding should be related to V8, and the increase in RSS should be normal but not unlimited. Currently, the memory growth in my demo test has slowed down significantly, and there are no OOM issues. I will continue to monitor this phenomenon.

The code for this native module is very simple, and there may be aspects I haven’t noticed, but in any case, it’s better than the situation with opencv4nodejs.

When the release method is not called, the following crash log appears:

setInterval(async () => {
  try {
    const full = await cv.imdecodeAsync(fs.readFileSync('src/dev/full.jpg'));
    full.release();
  } catch (e) {
    console.error(e);
  }
}, 0);

Fatal error in , line 0
Check failed: marking_done_.
Stack Trace:

----- Native stack trace -----
#
# Fatal error in , line 0
# Check failed: marking_done_.
#
#
#
#FailureMessage Object: 0x16ba63ae8
----- Native stack trace -----

 1: 0x1044c9340 node::NodePlatform::GetStackTracePrinter()::$_3::__invoke() [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
 2: 0x10554c9ac V8_Fatal(char const*, ...) [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
 3: 0x104786014 v8::internal::CppHeap::AllocatedObjectSizeIncreased(unsigned long) [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
 4: 0x1047bf8fc v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::internal::GarbageCollectionReason, char const*) [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
 5: 0x1047bd6f0 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
 6: 0x1047c6e64 v8::internal::Heap::FinalizeIncrementalMarkingIfComplete(v8::internal::GarbageCollectionReason) [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
 7: 0x1047d4bf4 v8::internal::IncrementalMarkingJob::Task::RunInternal() [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
 8: 0x1044c7778 node::PerIsolatePlatformData::RunForegroundTask(std::__1::unique_ptr<v8::Task, std::__1::default_delete<v8::Task>>) [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
 9: 0x1044c6494 node::PerIsolatePlatformData::FlushForegroundTasksInternal() [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
10: 0x104e35dec uv__async_io [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
11: 0x104e47ec4 uv__io_poll [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
12: 0x104e363b0 uv_run [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
13: 0x104395754 node::SpinEventLoopInternal(node::Environment*) [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
14: 0x1044a5c6c node::NodeMainInstance::Run(node::ExitCode*, node::Environment*) [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
15: 0x1044a5a08 node::NodeMainInstance::Run() [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
16: 0x10442f718 node::Start(int, char**) [/Users/william/.nvm/versions/node/v20.11.1/bin/node]
17: 0x18244a0e0 start [/usr/lib/dyld]