microsoft / microsoft/onnxruntime
[Bug] [Node.js binding] Memory leak after releasing inference session
@fs-eire is already working on this.
Since Jul 8, 2025.
- Dominant language
- C++
- Stars
- 21.9k
- Forks
- 4.2k
- Avg merge
- 4d 11h
- Merged PRs (30d)
- 184
Description
### Describe the issue
There's a memory leak occurring when creating an inference session using the runtime in Node. Each time a new session is created and released, the memory usage grows a little. We're running inference on an edge device with limited memory available, so after a couple sessions, the device runs out of memory. We need to reload the session, without restarting the whole application, as it's a realtime system.
Despite disabling "CpuMemArena" and "memPattern", as suggested for similar issues, the memory leak is still occurring.
### To reproduce
I've made a minimal javascript example that reproduces the issue:
``` Javascript
import * as ort from "onnxruntime-node";
const MODEL_FILE_PATH = "./model.onnx";
const getModel = async (filename) => await ort.InferenceSession.create(filename, { enableCpuMemArena: false, enableMemPattern: false });
const loadSession = async () => {
const session = await getModel(MODEL_FILE_PATH);
await session.release();
}
const main = async () => {
for (let i = 0; i < 10; i++) {
await loadSession();
}
}
(async () => {
await main()
})();
```
When testing locally, the application memory usage sits at 325 MB when loading the session once. When loading and releasing the session 10 times, it bloats to 994 MB. For 100 load/release cycles it sits at 9.12 GB
### Urgency
MEDIUM
### Platform
Mac
### OS Version
14.5
### ONNX Runtime Installation
Released Package
### ONNX Runtime Version or Commit ID
1.22.0-rev
### ONNX Runtime API
JavaScript
### Architecture
X64
### Execution Provider
Default CPU
### Execution Provider Library Version
_No response_
### Model File
_No response_
### Is this a quantized model?
Unknown
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.