MultiPage Tiff takes a lot of memory by MagickImageCollection

May 27, 2014 at 10:37 AM
Hello,

I have observed that while creating new MagickImageCollection object from multipage Tiff File(Tiff file having more than 50 pages in it). it used to take up lot of memory (around 2-3 GB).
Using images As New MagickImageCollection(FilePath)              
End Using
if we use Bitmap class to generate the object as per below code snippet it works fine and used to take only 20-30 MB.
Dim images as new Bitmap(Filepath)
Coordinator
May 27, 2014 at 11:36 AM
The Q16 version has HDRI enabled and uses 16 bytes per pixel. Magick.NET loads all the pixels and for big images or images with many pages this will require a lot of memory. It is possible the bitmap only uses 4 bytes per pixel. You could try switching to the Q8 version to reduce memory, it only uses 4 bytes per pixel. I am working on creating a separate Q16-HDRI version. The normal Q16 version will no longer use HDRI then and this will reduce the memory to 8 bytes per pixel.

And I think the Bitmap 'locks' the file and only loads the pixels when the active frame is set.
May 27, 2014 at 11:46 AM
Edited May 27, 2014 at 11:51 AM
Thanks for your quick reply.

Can we turnoff the HDRI or not to use 16 bytes per pixel mode based on some kind of checks in the same Q16 version.
Coordinator
May 27, 2014 at 12:07 PM
The HDRI setting is a build setting that cannot be changed. I hope I will be able to publish a new release that use 8 bytes this week. If this is urgent for you I might be able to provide you with a development build. Contact me through Codeplex on my profile page if it is really urgent.
Jun 2, 2014 at 1:17 PM
I have downloaded the latest normal build "Magick.NET-6.8.9.101-Q16-AnyCPU-net40-client.zip" from the download sections. With new build i have tried to test the multipage Tiff file with 50 pages in it but it still took more than a 1 GB of memory whereas .net Bitmap class doesn't take that much of memory.
Coordinator
Jun 2, 2014 at 1:29 PM
In a previous post I explained why Magick.NET will use more memory.

When you create a Bitmap it will lock the file on disk. I think the .NET bitmap only stores the active frame in memory or it will use the file from disk. Magick.NET will store all pixels in memory (8 bytes x 50 x [width] x [height] = ?? bytes).
Jun 2, 2014 at 1:41 PM
Thanks for your quick reply and explanation. Here, i have also created bitmap from file on the disk using MagickImage.

Is there any way we can achieve the performance using MagickImage or stores the active frame in memory or use the file from disk ?
Coordinator
Jun 2, 2014 at 2:10 PM
Edited Jun 2, 2014 at 7:30 PM
The architecture of Magick.NET is different from the .NET Bitmap and this cannot be changed.

You could limit the memory with the ResourceLimits class but that will create a new file on disk that contains the pixel data. This will really slow down your application but it will limit the use of memory.

A better solution would be reading a single frame from disk with the MagickReadSettings class:
MagickReadSettings settings = new MagickReadSettings()
{
  FrameIndex = 0, // Change this number to set the start index of the pages.
  FrameCount = 1 // Change this number to set the number of pages.
};

using (MagickImage image = new MagickImage("your.tif", settings))
{
  image.Write("your.firstpage.tif");
}
This will be slower than reading the whole image at once but it will reduce the amount of memory being used.