This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

IFrame GetBitmap using too much memory?

I have a bitmap (dimensions 2688x3581x24bpp) which I load in via an IFrame. I do this because I want to show the metadata about the image in my UI without loading the 27MB of image to do it. IFrame allows me to do this.

I do however need to load this image at times, and so as I already have a reference to the IFrame I call GetBitmap on the IFrame.

Bitmap bitmap = new Bitmap();
frame.GetBitmap(bitmap);

Now prior to the GetBitmap() call my memory usage (measured using performance counters Process/Private Bytes) my memory usage is 37MB. The Bitmap takes up 27MB (according to my calculations and the MemoerySize property). After GetBitmap my memory usage is around 120MB. Even after calling GC.Collect()/WaitForPendingFinalizers()/Collect().

I've written a test app which dumps the memory usage to screen -

Start memory Usage
39.17578
Loaded to IFrame
60.85547
Created Empty Bitmap
62.375
Called GetBitmap()
117.832
Called GC.Collect()/WaitForPendingFinalizers()/Collect()
117.8438

The Call to GetBitmap makes memory jump 55MB - very close to 2*27MB! Any thoughts/ideas? I can share my code if required.
The source for the above issue can be downloaded here

Download

Its purely intended to show the issue, and I'm fully aware that I'm not freeing all my own resources, although at the point of writing the memory usage this isnt important.
Hello,

Good question! Yeap, these 55Mb are 2 * 27Mb. The reason is that IFrame object stores its own copy of the decoded Bitmap inside. Here is output of your test application with a little modification:

Start memory Usage
24.65625
Loaded to IFrame
29.83203
Created Empty Bitmap
32.29688
Called GetBitmap()
87.67969
Called frame.Dispose()
60.24219
Called bitmap.Dispose()
32.76563


Such memory overhead is necessary to allow code like this:
FormatReader r = <somehow_create_reader()>;
IFrame frame = r.LoadFrame(0);
Bitmap bmp = new Bitmap();
frame.GetBitmap(bitmap);
<... do some modifications to bitmap ...>

//And after that:
FormatWriter w = <somehow_create_writer()>;
w.AddFrame(frame); //Store original image without any modifications.

//Or: 
Bitmap another_bmp = new Bitmap();
frame.GetBitmap(bitmap); //Immediatly get another clean copy of frame image.

In your case you should extract all necessary info from IFrame object and dispose it as soon, as possible.
OK, cheers.

It looks like I'll use IFrame for loading metadata, and manually load a Bitmap outside IFrame if I require one.

N