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

System.OutOfMemoryException: Optimization suggestions? #873

Closed
deivydas321 opened this issue Apr 4, 2019 · 4 comments
Closed

System.OutOfMemoryException: Optimization suggestions? #873

deivydas321 opened this issue Apr 4, 2019 · 4 comments

Comments

@deivydas321
Copy link

deivydas321 commented Apr 4, 2019

Hello,
I have this method where I resize image(max size 10MB) to 1 MB.

public static Stream ResizeToTargetFileSize(IFormFile file, long targetBytes)
       {
           var imageFormat = ImageFormatFactory.GetImageFormat(file.ContentType, file.FileName);
           var outputStream = new MemoryStream();
           using (var stream = file.OpenReadStream())
           using (var image = Image.Load(stream))
           {
               if (stream.Length > targetBytes)
               {
                   var newSize = GetScaledSize(image, imageFormat.GetFactor(stream.Length, targetBytes));
                   image.Mutate(x => x.Resize(newSize));
               }
               image.Save(outputStream, imageFormat.Encoder);
           }
           outputStream.Position = 0;
           return outputStream;
       }

However, when I upload file 9.7MB size I am getting memory error. Are there any suggestions how to optimize memory by changing this code?
New Size is Width * 0.3, Height*0.3.

The image:
Pizigani_1367_Chart_10MB

@JimBobSquarePants
Copy link
Member

JimBobSquarePants commented Apr 4, 2019

Please see #805

@antonfirsov
Copy link
Member

also #642

@deivydas321
Copy link
Author

deivydas321 commented Apr 5, 2019

I checked the mentioned issues but didn't find any proper solution or workaround. I am working on ASP .NET CORE app and when I upload and resize images(size 10 MB) the application memory instantly increases to 2-3GB would be there any suggestions how to avoid this? I have tried memory management methods from SixLabors/docs/blob/master/articles/ImageSharp/MemoryManagement.md page but it didn't seems to help. The code for resizing is the same as above.
image

@JimBobSquarePants
Copy link
Member

Questions on the Gitter channel please. We’re really clear about this in the issue template you deleted.

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

No branches or pull requests

3 participants