-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
appears as if gc not freeing values created via node-api & napi-rs #5659
Comments
I spent some time investigating and didn't make much progress. The repro is very helpful though. I do think there is a memory leak in napi_ref, it needs to be deleted. But that is not the cause of this bug. There are finalizers being called. But the one we care about here -- the one holding the memory for the big byte array -- is not being freed and I'm not sure why. It's likely something to do with our implementation of Napi classes. It's not something to do with napi_wrap or napi_unwrap. |
In #7765, it should now free the values as expected. It looks like we still use lots of ram though. if the code is changed to move the const pl = require("nodejs-polars");
function runBuggyCode(entries) {
let df = pl.DataFrame(entries);
}
async function main() {
let count = 0;
let peakMemUsage = 0;
while (count < 500) {
const start = Date.now();
const data = Array(50)
.fill(null)
.map((_, i) =>
Array(10000)
.fill(0)
.map((_, ii) => i * 100 + ii)
);
runBuggyCode(data);
console.log("process time:", Date.now() - start, "ms");
peakMemUsage = Math.max(process.memoryUsage().rss, peakMemUsage);
console.log(
"peak rss mem usage:",
Math.round(peakMemUsage / 1024 ** 2),
"MB"
);
console.log("run:", count++);
await new Promise((res) => setTimeout(res, 100));
}
}
void main().catch((err) => console.error(err)); I suspect it's a timer execution order issue |
I'd expect the former to use less memory. in your example, we are creating the array inside the loop. It seems highly unexpected that it'd use less ram creating a large array in a loop than creating it outside. |
What version of Bun is running?
1.0.1+31aec4ebe325982fc0ef27498984b0ad9969162b
What platform is your computer?
Darwin 22.4.0 arm64 arm
What steps can reproduce the bug?
originally opened in nodejs-polars, but seems to behave a bit differently in bun.
What is the expected behavior?
memory usage doesn't keep growing
What do you see instead?
memory grows on every iteration
Additional information
Nodejs: peak rss mem usage: 207 MB
Bun: peak rss mem usage: 2111 MB
mem stats
I tried adding a custom finalize and it seems like it'll never get called in bun, but it does get called in nodejs.
Also wanted to say great work on the FFI. For nodejs-polars, which needs to iterate over very large arrays, it is >10x faster for creating dataframes & series! while also using less memory!! Great work 🚀 🚀 !!
pola-rs/nodejs-polars#69
The text was updated successfully, but these errors were encountered: