Load image from file without loading it into memory

Discussions about image processing and document imaging.
Post Reply
risotoh985
Posts: 10
Joined: Mon Nov 11, 2019 8:57 pm

Load image from file without loading it into memory

Post by risotoh985 » Mon Nov 11, 2019 9:09 pm

Hi,

testing your library at the moment and wonder why CreateGdPictureImageFromFile creates almost exactly the same memory footprint whether I set LoadInMemory to True or False.

I use this very simple one line call:

Code: Select all

var imageId = _gdPictureImaging.CreateGdPictureImageFromFile("LargeImage.jpg", false);
I see almost zero difference from the LoadInMemory parameter.

In your documentation I see no hint of e. g. only certain image file formats being supported for this or other things that I should take care of to make "LoadInMemory=False" work.
What am I doing wrong?

Thanks
riso

loicaigon
Posts: 35
Joined: Mon Oct 21, 2019 9:11 am

Re: Load image from file without loading it into memory

Post by loicaigon » Tue Nov 12, 2019 9:58 am

Hi Riso,

Let me check with the dev team and get back to you.

Best regards,

Loic Aigon

risotoh985
Posts: 10
Joined: Mon Nov 11, 2019 8:57 pm

Re: Load image from file without loading it into memory

Post by risotoh985 » Tue Nov 12, 2019 10:02 am

Thanks a lot, Loic!

risotoh985
Posts: 10
Joined: Mon Nov 11, 2019 8:57 pm

Re: Load image from file without loading it into memory

Post by risotoh985 » Thu Nov 14, 2019 11:11 am

Hi Loic,

sorry for bothering, but did you get any feedback from your developers already?

thanks
riso

loicaigon
Posts: 35
Joined: Mon Oct 21, 2019 9:11 am

Re: Load image from file without loading it into memory

Post by loicaigon » Thu Nov 14, 2019 3:39 pm

Hi Riso,

You don't bother ;)

LoadInMemory is related to the file size. When you say: "I see almost zero difference", I presume that you expect a much bigger difference with using true than false. However is the file is light, the memory consumption may not widely differ whatever you are using true or false.
To confirm that, try loading a heavy file. You should then notice a much bigger difference.

I hope this answers your question. Otherwise, I may suggest that you send a support request to our services.

Best Regards,
Loic Aigon

risotoh985
Posts: 10
Joined: Mon Nov 11, 2019 8:57 pm

Re: Load image from file without loading it into memory

Post by risotoh985 » Fri Nov 15, 2019 1:38 pm

Hi Loic,

thanks for getting back to me, appreciating your efforts.

I've attached you a large, but just white JPG file to this post. I would like to ask you to run these two lines of code with it:

Code: Select all

for (var i = 1; i <= 10; i++)
                var imageId = _gdPictureImaging.CreateGdPictureImageFromFile("LargeImage.jpg", true);
It just loads this file 10 times and the LoadInMemory parameter is set to true:
The result is an increase in memory usage by the process of about 377 MB which is expected as the file attached has a size of roughly 34 MB when saved in bitmap file format - so it makes sense that it has approximately the same size (34 x 10 = 340 MB) in memory.

Now please run the same two lines of code with the LoadInMemory parameter changed to false:
The result is that the memory usage by the process is now 373 MB, just 4 MB (< 1 % difference!) less than before.

So although LoadInMemory is set to false GdPicture obviously still loads and extracts the image into memory without any compression applied (this blank image would be highly compressible obviously, still is has the uncompressed bitmap size in memory).

From the documentation I understand that using LoadInMemory=false would make GdPicture reading image data always from the file, thus being slower, of course, but no big memory footprint needed for the image. At the moment though it looks like there is no difference if this parameter is set to true or false as in both cases the full image is loaded (even uncompressed) into memory.

It would be great if you could discuss this with your developers again - at least to me it looks like this parameter is currently not working correctly or at least as it is expected to work (based on your documentation).

Thanks!
Riso
Attachments
LargeImage.zip
(807 Bytes) Downloaded 543 times

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Load image from file without loading it into memory

Post by Loïc » Fri Nov 15, 2019 6:42 pm

Hi,

Your measurement is probably incorrect. .NET memory management is based on a garbage collector so you can not run into such conclusion by simply comparing memory used by the process.
What I can say so far is the method is doing what it is supposed to do, in a correctly way. You can also implement your own "load into memory" feature by simply loading the file content into a memory stream, then use the overload taking a stream as parameter to open the bitmap.

If you want to accurately profile the memory you should learn about how .NET garbage collector works and use a memory profiling tool.

With best regards,

Loïc Carrère

risotoh985
Posts: 10
Joined: Mon Nov 11, 2019 8:57 pm

Re: Load image from file without loading it into memory

Post by risotoh985 » Fri Nov 15, 2019 7:39 pm

Hi Loïc,

no worries, I do know how garbage collection works, but this does not really matter here as GdPicture loads the image data into unmanaged memory which is obviously not effected by garbage collection.

To verify this I just ran my small example project (attached now) using JetBrains dotMemory Profiler with expected results:
GdPicture-MemorySnapshot.PNG
The first snapshot was captured directly after executing the program and the second one after pressing the button "Load image 10 times".
As you can easily see unmanaged memory increased by 338,65 MB whereas the managed memory stayed basically the same.

So despite the LoadInMemory parameter is set to false GdPicture obviously loads the full uncompressed image into unmanaged memory.

Is this really the expected behavior of setting LoadInMemory=false?

Thanks and enjoy the weekend :-)
Riso
Attachments
GdPictureTest3.zip
(28.18 KiB) Downloaded 527 times
GdPictureTest3.zip
(28.18 KiB) Downloaded 533 times

loicaigon
Posts: 35
Joined: Mon Oct 21, 2019 9:11 am

Re: Load image from file without loading it into memory

Post by loicaigon » Mon Nov 18, 2019 1:21 pm

Hi Riso,

We had a double check on our side and there is nothing wrong with LoadInMemory method that we can identify. The results you get are likely to be related to internal .Net memory management.

I may suggest that you address this concern in the .net Foundation Forum where other developers may have already experienced such memory issues
https://forums.dotnetfoundation.org/

Best regards,

Loic Aigon

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Load image from file without loading it into memory

Post by Loïc » Fri Nov 22, 2019 8:46 pm

Hi,

>>So despite the LoadInMemory parameter is set to false GdPicture obviously loads the full uncompressed image into unmanaged memory.
Sure, this is expected. The feature is about loading the uncompressed document into the memory before decoding it. Comparing memory usage in both context is super tricky because of the different caching strategies of the .NET framework and GdPicture (which is adaptive based on the bandwidth, memory usage and other statistics created on the fly).

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest