Reputation: 203
I'm encountering out of memory exceptions when i resize images 9000x9000 square using ImageResizer.Net (on a 32-bit system):
ImageBuilder.Current.Build(imageFileName, outputFileName, settings, true);
I am able to successfully resize the large images using a stream though:
using (var stream = new FileStream(imageFileName, FileMode.Open, FileAccess.Read))
using (var img = Image.FromStream(stream, true, false))
{
ImageBuilder.Current.Build(img, outputFileName, settings);
stream.Close();
}
but, this last method still hits an out of memory exception after x loops. Is there a huge memory leak in ImageResizer, or is there an error in my code?
Either way, is there a workaround?
Upvotes: 4
Views: 3523
Reputation: 16468
You need a 64-bit system if you're going to be processing 81 megapixel images. Just decompressing the image will require between 350 and 800 contiguous megabytes of RAM.
On a 32-bit system (even with 16GB of ram installed), only 1200MB or so are initially available to any .NET process. Due to fragmentation (not memory leaks!) that 1200MB will be split up into small 50-100MB chunks by any activity. Since you need your memory in 800MB blocks (since you're processing massive images), that stops working quickly.
To allow .NET to combat memory fragmentation, you need to give it (a) time and (b) plenty of extra space.
On a 64-bit system, the process should be able to access enough RAM for the .NET runtime to not starve under these workloads.
Upvotes: 6
Reputation: 9791
As mentioned by Computer Linguist in a comment on their own answer libvips is capable of resizing large images while using (much) less memory. [I actually ended-up using the command line version as I didn't come across any .NET wrappers for the library itself.]
In my case, the image I was trying to resize was 21,920 × 14,610 pixels. ImageResizer successfully resized a PNG version (15+ MB) of the image but threw an OutOfMemoryException
for a JPEG version (22+ MB).
I'm running Windows 7 64-bit on a computer with 8 GB of RAM.
Upvotes: 0
Reputation: 85
I would think you're maxing out your RAM with some of the large image resizing (not sure though).
Try to use the DiskCache plugin: http://imageresizing.net/plugins/diskcache http://imageresizing.net/download
It will write the resized images directly to disk bypassing any RAM issues. Plus it's really fast too.
Im sorry to answer with a "try this" comment, as answer, but I'm unable to write comments just yet due to insufficient SO reputation.
Upvotes: 0