Skip to content
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

getImageData method memory leak? #819

Open
fancymiracle opened this issue Apr 25, 2024 · 3 comments
Open

getImageData method memory leak? #819

fancymiracle opened this issue Apr 25, 2024 · 3 comments

Comments

@fancymiracle
Copy link

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
Copy link
Owner

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

@fancymiracle
Copy link
Author

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
Copy link

adraffy commented Apr 29, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants