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

memory leak on big size image #105

Open
hodev-dev opened this issue Jan 30, 2021 · 3 comments
Open

memory leak on big size image #105

hodev-dev opened this issue Jan 30, 2021 · 3 comments
Labels
algorithm Relates to the BlurHash algorithm bug Something isn't working question Further information is requested

Comments

@hodev-dev
Copy link

I have 10 MB image so when i use PHP implementation, 12 GB of memory get max and kills the process

thsi is class

class BlurHashHelper
{
    public static function encode($file_path)
    {
        $image = imagecreatefromstring(file_get_contents($file_path));
        $width = imagesx($image);
        $height = imagesy($image);
    
        $pixels = [];
        for ($y = 0; $y < $height; ++$y) {
            $row = [];
            for ($x = 0; $x < $width; ++$x) {
                $index = imagecolorat($image, $x, $y);
                $colors = imagecolorsforindex($image, $index);
    
                $row[] = [$colors['red'], $colors['green'], $colors['blue']];
            }
            $pixels[] = $row;
        }
    
        $components_x = 4;
        $components_y = 2;
        $blurhash = Blurhash::encode($pixels, $components_x, $components_y);
        $pixels = [];
        return $blurhash;
    }
}
@xpire
Copy link

xpire commented Jun 8, 2021

I think you're not meant to apply the algorithm onto a large image directly, it should be scaled down first as per README.

@hodev-dev
Copy link
Author

I'm mot expert but as web going to support 4k and HIDPI more and more.we need higher resolution image .It would be better if we change algorythm to use stream or buffer for biger size images

@sngrl
Copy link

sngrl commented Aug 11, 2021

I don't use BlurHash, but have the similar problem - huge memory leak when using imagecolorat() function. I call imagecolorat() for each pixel of the image (via two nested cycles, as at code example at first message) - it's required for my business logic process.
I process many images in cycle at one process, and I call imagedestroy() after finish of each image processing, but unfortunately memory still leaked.
If I comment line with imagecolorat() calling memory leak is disappear. So I think that after calling imagedestroy() memory, used for store result of the imagecolorat() work, still full of data and not purged until php-process was not finished or killed.
I have no idea what we can to do with that.

Added a little bit later:

When you are using unset, the memory will only be freed whenever garbage collector decides, but when you are setting a variable to a different value (null in this case), then you might get some memory freed of course with the cost of CPU.

So, I tried change my code from imagedestroy($resource) to $resource = null and memory leak was gone.
I hope this will be useful to someone.

@jerry-git jerry-git added algorithm Relates to the BlurHash algorithm bug Something isn't working question Further information is requested labels Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
algorithm Relates to the BlurHash algorithm bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants