Brooooooklyn / canvas

High performance skia binding to Node.js. Zero system dependencies and pure npm packages without any postinstall scripts nor node-gyp.
https://vercel.skia.rs
MIT License
1.74k stars 76 forks source link

getImageData method memory leak? #819

Open fancymiracle opened 5 months ago

fancymiracle commented 5 months ago

let canvas1 = createCanvas(1920, 1080) let ctx = canvas1.getContext('2d') for(var i=0;i<5000;i++){ const rawFrameImage = ctx.getImageData(0, 0, 1920, 1080).data; console.info(process.memoryUsage().rss / 1024 / 1024 + " MB"); clearAllCache(); }

The memory is not released.

Brooooooklyn commented 5 months ago

@fancymiracle await a setTimeout before the console.info, the Node.js never had a chance to run gc in your code

fancymiracle commented 5 months ago

Sorry, let me provide another example. Manually, every time a press occurs, it is sent from the renderer thread to the main thread as follows:


const { createCanvas, clearAllCache } = require('@napi-rs/canvas')

let canvas1 = createCanvas(1920, 1080)
let ctx = canvas1.getContext('2d')
ipcMain.on('toMain', (event, args) => {
    const rawFrameImage = ctx.getImageData(0, 0, 1920, 1080).data;
    console.info(process.memoryUsage().rss / 1024 / 1024 + " MB");
    clearAllCache();
})

Even after repeating this process several times, the memory continuously increases, and it does not decrease as observed in the Activity Monitor.

But if replace getImageData with canvas1.data(), which also retrieves the entire frame data, no memory leak occurs. However, this way, can only retrieve the entire frame rather than parts of it.

adraffy commented 5 months ago

Probably related: drawImage() is leaking in 0.1.52 with globalCompositeOperation = copy