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

[bug fix] Clear unused canvases to avoid memory leak. #191

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hedgerwang
Copy link

@hedgerwang hedgerwang commented May 6, 2023

This resolved the issue where some canvases were not cleared properly, and it might result in reaching the limit of maximum canvas memory usage when the client attempted to compress multiple images.

@hedgerwang
Copy link
Author

cc @Donaldcwl

@hedgerwang hedgerwang changed the title Clean unused canvases to avoid memory leak. Clear unused canvases to avoid memory leak. May 6, 2023
@hedgerwang hedgerwang changed the title Clear unused canvases to avoid memory leak. [bug fix] Clear unused canvases to avoid memory leak. May 6, 2023
The function `cleanupCanvasMemory` does not really cleans up the memory when the canvas size is set as `0`. The right way to clean up the memory is to size the size to `1` instead.
// eslint-disable-next-line no-param-reassign
canvas.height = 0;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the width or height to 0 doesn't actually free up memory. Instead, it makes the canvas context unavailable to the canvas, while the associated pixel data remains preserved in memory. The correct method for clearing memory is to set the size to 1.

Refer to this blog post for more information: https://pqina.nl/blog/total-canvas-memory-use-exceeds-the-maximum-limit/

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

Successfully merging this pull request may close these issues.

None yet

1 participant