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

Out of Memory Exception when resizing image #409

Closed
3 of 4 tasks
ghost opened this issue Dec 21, 2017 · 4 comments
Closed
3 of 4 tasks

Out of Memory Exception when resizing image #409

ghost opened this issue Dec 21, 2017 · 4 comments

Comments

@ghost
Copy link

ghost commented Dec 21, 2017

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: ImageProcessorFunctions.ProcessImageConversion ---> SixLabors.ImageSharp.ImageProcessingException : An error occured when processing the image using DelegateProcessor`1. See the inner exception for more detail. ---> SixLabors.ImageSharp.ImageProcessingException : An error occured when processing the image using ResizeProcessor`1. See the inner exception for more detail. ---> System.OutOfMemoryException : Exception of type 'System.OutOfMemoryException' was thrown.
 at System.Buffers.ConfigurableArrayPool`1.Rent(Int32 minimumLength)
 at SixLabors.ImageSharp.Memory.Buffer`1..ctor(Int32 length)
 at SixLabors.ImageSharp.Processing.Processors.ResizeProcessor`1.OnApply(ImageFrame`1 source,ImageFrame`1 cloned,Rectangle sourceRectangle,Configuration configuration)
 at SixLabors.ImageSharp.Processing.CloningImageProcessor`1.CloneAndApply(Image`1 source,Rectangle sourceRectangle) 
 End of inner exception
 at SixLabors.ImageSharp.Processing.CloningImageProcessor`1.CloneAndApply(Image`1 source,Rectangle sourceRectangle)
 at SixLabors.ImageSharp.Processing.CloningImageProcessor`1.Apply(Image`1 source,Rectangle sourceRectangle)
 at SixLabors.ImageSharp.DefaultInternalImageProcessorContext`1.ApplyProcessor(IImageProcessor`1 processor,Rectangle rectangle)
 at SixLabors.ImageSharp.DefaultInternalImageProcessorContext`1.ApplyProcessor(IImageProcessor`1 processor)
 at SixLabors.ImageSharp.ImageExtensions.<>c__DisplayClass134_0`1.<Resize>b__1(IImageProcessingContext`1 x)
 at SixLabors.ImageSharp.ImageExtensions.Mutate[TPixel](Image`1 source,Action`1 operation)
 at SixLabors.ImageSharp.ImageExtensions.<>c__DisplayClass134_0`1.<Resize>b__0(Image`1 img)
 at SixLabors.ImageSharp.Processing.DelegateProcessor`1.BeforeImageApply(Image`1 source,Rectangle sourceRectangle)
 at SixLabors.ImageSharp.Processing.ImageProcessor`1.Apply(Image`1 source,Rectangle sourceRectangle) 
 End of inner exception
 at SixLabors.ImageSharp.Processing.ImageProcessor`1.Apply(Image`1 source,Rectangle sourceRectangle)
 at SixLabors.ImageSharp.DefaultInternalImageProcessorContext`1.ApplyProcessor(IImageProcessor`1 processor,Rectangle rectangle)
 at SixLabors.ImageSharp.DefaultInternalImageProcessorContext`1.ApplyProcessor(IImageProcessor`1 processor)
 at SixLabors.ImageSharp.ImageExtensions.Resize[TPixel](IImageProcessingContext`1 source,Int32 width,Int32 height,IResampler sampler,Rectangle targetRectangle,Boolean compand)
 at SixLabors.ImageSharp.ImageExtensions.Resize[TPixel](IImageProcessingContext`1 source,Int32 width,Int32 height)
 at Kyobi.WebJob.ImageProcessor.ImageProcessorFunctions.<>c__DisplayClass4_0.<ConvertImage>b__0(IImageProcessingContext`1 x) at C:\Users\rbaker\git\kyobi_backend\src\webjobs\Kyobi.WebJob.ImageProcessor\ImageProcessorFunctions.cs : 98
 at SixLabors.ImageSharp.ImageExtensions.Mutate[TPixel](Image`1 source,Action`1 operation)
 at async Kyobi.WebJob.ImageProcessor.ImageProcessorFunctions.ConvertImage(Stream input,Stream output,FormatTemplate template,String extension,TextWriter log) at C:\Users\rbaker\git\kyobi_backend\src\webjobs\Kyobi.WebJob.ImageProcessor\ImageProcessorFunctions.cs : 98
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at async Kyobi.WebJob.ImageProcessor.ImageProcessorFunctions.ProcessImageConversion(ImageConverterJob job,Stream input,CloudBlockBlob outputBlob,TextWriter log) at C:\Users\rbaker\git\kyobi_backend\src\webjobs\Kyobi.WebJob.ImageProcessor\ImageProcessorFunctions.cs : 60
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at async Microsoft.Azure.WebJobs.Host.Executors.VoidTaskMethodInvoker`2.InvokeAsync[TReflected,TReturnType](TReflected instance,Object[] arguments) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\VoidTaskMethodInvoker.cs : 20
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at async Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync[TReflected,TReturnValue](Object instance,Object[] arguments) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs : 63
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeAsync(IFunctionInvoker invoker,ParameterHelper parameterHelper,CancellationTokenSource timeoutTokenSource,CancellationTokenSource functionCancellationTokenSource,Boolean throwOnTimeout,TimeSpan timerInterval,IFunctionInstance instance) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs : 583
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstance instance,ParameterHelper parameterHelper,TraceWriter traceWriter,CancellationTokenSource functionCancellationTokenSource) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs : 534
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs : 477
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs : 264 
 End of inner exception
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(??) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs : 320
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsync(IFunctionInstance functionInstance,CancellationToken cancellationToken) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs : 96

Steps to Reproduce

sizes used:

Width Height
360 202
540 303
720 404
1080 606
1440 808
750 422
1242 699

private async Task ConvertImage(Stream input, Stream output, FormatTemplate template, string extension, TextWriter log)
        {
            using (var image = Image.Load(input))
            {
                if (image.Width == template.Width && image.Height == template.Height)
                {
                    //Nothing to resize, just provide the output
                    await log.WriteLineAsync("Found image to be the correct size already; saving directly to ouput...");
                    input.Position = 0;
                    input.CopyTo(output);
                }
                else
                {
                    await log.WriteLineAsync($"Resizing image to {template.Height} by {template.Width}...");
                    image.Mutate(x => x.Resize(template.Width, template.Height));

                    await log.WriteLineAsync($"Saving {extension} image...");
                    if (extension.Equals("png", StringComparison.OrdinalIgnoreCase))
                    {
                        image.Save(output, new PngEncoder { Quantizer = new WuQuantizer<Rgba32>(),  PngColorType = PngColorType.Palette });
                    }
                    else
                    {
                        image.Save(output, new JpegEncoder());
                    }
                }
            }
        }

I used these images:

https://static.pexels.com/photos/248797/pexels-photo-248797.jpeg
file:///C:/Users/rbaker/Desktop/rose-blue-flower-rose-blooms-67636.jpeg

System Configuration

  • ImageSharp version: 1.0.0-beta2
  • Environment (Operating system, version and so on): Azure Webjob
  • .NET Framework version: .netcore 2
@JimBobSquarePants
Copy link
Member

Hi @robertbaker

I can see in your stacktrace you are running in Azure. (Thanks for that!)

Are you running in 32bit mode? If so I cannot recommend that as you will too easily run out of contiguous memory.

Cheers

James

@garethterrace
Copy link

Is there any way of detecting the maximum image size supported in 32bit instances so we can catch this? I'm having the same issue on 32bit azure and don't want to switch to 64bit

@JimBobSquarePants
Copy link
Member

No. I'm afraid not. You have a maximum amount of contiguous memory, 1Gb i think, in 32bit mode that is shared across any running applications. We work hard to limit our usage but can only ask out of what is available. I would never recommend running a website that did any image processing in 32bit mode.

Why would you not want to switch?

@ghost
Copy link
Author

ghost commented Jan 4, 2018

Thank you, I was running in 32bit, I switched to 64 bit mode and no problems.

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

2 participants