1

I have the following lines of code in a function. which reads the image from Amazon S3. Image size which i am reading is of 1.37 MB where as when i ran the profiler it says read function in image magick library takes 5.6 mb which is very high. Can anyone explain this behaviour? I am attaching the snapshot of my profiler as well as code.

 AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(
                         accessKey,
                         secretKey
                        );

                GetObjectRequest request = new GetObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName
                };

                var response = client.GetObject(request);                
                MagickImage imgStream = new MagickImage(response.ResponseStream);

enter image description here

Naresh
  • 5,073
  • 12
  • 67
  • 124

1 Answers1

3

Your image size on disk is not important for the size of the image in memory. The amount of memory that is necessary is related to the dimensions (width/height) of your image. When the image is loaded the raw data is 'converted' to pixel data. For each channel Magick.NET will use either 8 or 16 bit per pixel (Q8/Q16). So when you have an image that is 4 channels (RGBA) and you are using the Q16 version of Magick.NET you will use 64-bits per pixel. For an image of 1920x1080 you will need 1920*1080*64 = 132710400 bits, and that is around 16.5 Megabytes. The size on disk will be smaller most of the times because most image formats compress the pixel data when they save it to disk.

dlemstra
  • 7,813
  • 2
  • 27
  • 43
  • Thanking you for your explanation. Could you please also give me some reference for the GO lang for doing the same. Moreover, performance wise will GO version of Image Magick would be better or Magick.Net – Naresh Jan 12 '16 at 12:10
  • I don't have experience with the GO wrapper for ImageMagick and you will probably have to do the performance comparison yourself. – dlemstra Jan 12 '16 at 12:12
  • Can you explain in more detail how does it differ when you store the image in memory as compared to disk. Is this in general happen when load the image in memory it bloat up. – Naresh Jan 12 '16 at 13:02
  • I added a simple explanation to the answer and yes most of the times it will use more in memory then on disk due to compression. – dlemstra Jan 12 '16 at 21:49
  • I have also seen memory utilization keeps on increasing in this code. Even after disposing the client and image magick object. Could you also please tell the reason why is it so. On production server when application pool recycles on a daily basis. It gets reduced. Is there any memory leak in the library or this is normal behaviour – Naresh Jan 15 '16 at 09:55
  • Your code does not include a using statement. MagickImage is IDisposable so you should dispose it after using it. – dlemstra Jan 15 '16 at 10:01
  • Yes, I do that explicitly `imgStream .dispose()` as well as `client.dispose()`. But even after that i keeps on increasing. On top of that i also assigned null explicitly `imgStream = null` and `client = null`.But still couldn't get rid of that. – Naresh Jan 15 '16 at 10:34