samizdatco / skia-canvas

A GPU-accelerated 2D graphics environment for Node.js
MIT License
1.73k stars 69 forks source link

v1.0.1 - Apparent memory leak with async output in some conditions #145

Closed mpaperno closed 1 week ago

mpaperno commented 1 year ago

Hello!

I've been tracking down a memory leak with a program I work on which uses skia-canvas. I tracked the issue to s-c and I seem to have narrowed it down to some difference in v1.0.1 vs. 1.0.0.

I've also narrowed down that it only occurs when:

So using async version causes a leak regardless of GPU option, but using the sync version and GPU rendering does not.

More specifically, seems to be caused by 68bef1bdb43f26228090c2858a6dfe74e72eeadf. At the commit prior to that I cannot recreate it (detailed observations below). Why this has anything to do with async operation, I have no idea, but my results were clear and reproducible.

I'm attaching my test script (and a basic package file). It works with skia-canvas versions going back to at leat 0.9.25 and has no other dependencies. Async and GPU (for v1+) can be en/disabled on command line or with variables at the top. The image output is discarded, no files are written... there's a little running time counter to show the script is working. CTRL-C to exit.

Copied from the comments in the test script, here are my observations (memory counts are just for reference, YMMV on various systems of course).

// Observed memory (measurements from "active set" counter in Win10 Task Manger > Details):
//
// v1.0.1rc1 - 1.0.1:
//         ASYNC on,  GPU any == LEAK, steady increase until exit
//         ASYNC off, GPU off == LEAK,  "
//         ASYNC off, GPU on  == hovers ~65MB
// ---------------------------------------
// v1.0.0 - skia-canvas@9f35d190122e1aaa85469d2a74044ee026071e47
//         ASYNC on,  GPU off == hovers ~45MB
//         ASYNC on,  GPU on  == hovers ~160MB
//         ASYNC off, GPU off == hovers ~43MB
//         ASYNC off, GPU on  == hovers ~65MB
// ---------------------------------------
// v0.9.25 - v0.9.30:
//         ASYNC on or off  == climb to just < ~300MB then sharp reset to ~60MB, repeat
// ---------------------------------------

My test system is Windows 10, Intel CPU (no GPU), NVIDIA RTX2080Ti with Studio driver 536.40 (first reported by another user with AMD CPU, NVIDIA RTX3xxx on Win10). Node v18.16.0. I haven't tried it on a system with non-supported GPU yet, but that's on my list.

If anyone else cares to give the test script a shot with the different settings, that'd be great. I spent quite a while verifying my results, but ya never know. It can take a bit of time before the leak is obvious, patience and comparison to "non-leaky" results are helpful.

Please let me know if I can provide any further details.

Thanks! -Max

skia-canvas-mem-test.zip

PS. I also posted the script as a Gist, to see w/out a download. Not much to it. https://gist.github.com/mpaperno/75284be4de0ef2006d876773507e6ddf

Santas commented 3 months ago

Just FYI for others that considered holding off updating, because of this issue - test on your machines. I wasn't able to reproduce the issue on Mac from the gist with async=on, gpu=any. Also we don't have an issue running in production on Windows machines with steady load generating about 17M images/day - we use async and gpu off.

samizdatco commented 1 week ago

addressed (and hopefully fixed?) in #172