PictureBox image + Garbage cleanup problem . .. Please help

C

CroDude

Hi all!

I've made a little app that creates thumbnails and if user clicks on it, it
displays the original picture in the picturebox on a panel control.
In a thumbnail mouse down event I'm using static method
Image.FromFile(string path).Something like this:

this.mainApp.pictureBox1.Image = Image.FromFile( imagePath )

Well ... the problem starts when loading a image 3200x2400x24bit which is
about 1.7 MB on disk, it takes approximately 25MB RAM when loaded??? Why ???
The problem gets worse if user clicks on the multiple thumbs(every image
thumb represents is 3200x2400) in a short amount of time.
As user clicks on each thumb, the memory usage is pilling up. For every
1.7MB picture for about 25MB. Usually GC starts cleanup near 1GB when it
hits the ceiling of my system of 1GB.
Now why GC cleans up the memory so infrequent and waits till all resources
are nearly full?
Am I missing something here. I can't believe I'm stuck on somethig stupid
like this.

Please help me, I'm near breakdown!

Thanks in advance! and Marry Christmas all!

My specs:
Athlon Barton 2.5
1 GB RAM
Win2k sp4
DOT Net 2beta
 
B

Bob Powell [MVP]

You have to remember that images are often stored on disk in a compressed
format. JPEG compression is very efficient, if lossy, and enables quite
significant savings in file-size over real-size of an image.

When calculating the real memory requrements of an image you need to
consider the equation h*w*d/8 which is height times width times bit-depth
divided by 8.

An image you suggest of 3200*2400*24 bits requires over 23 megabytes to
store in its uncompressed raster form. No surprise then that your app
requires 25 megs.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
N

Nurchi BECHED

Hey,
Just a note, take your 1.7 Mb image file and save it as .BMP file.
You will see, that it takes around 20-25 Mb on disk as well.
If JPEG takes 1.7Mb, it should be either awesome quality picture,
or just wase of space (which is probably not the case).

Then compress your BMP with any archiver (ZIP, RAR, ARJ, ACE, CAB, etc)
ZIP or RAR are perhaps the most popular and then look at the filesize.
It should be under 3 megs. Most probably even close to your 1.7 Mb.

So, no wonder, why it takes 25 megs in memory, and to fight that,
you can call garbage collector manually somehow.
I never had to use it, so just play with subpackages of "System."
Or just ask someone else.

AFAIK, (please correct me if I am wrong) GC does the job only when it
"needs" to be done, i.e. if you have half-gig ram still available AND
the CPU is "busy" with some other processes, GC will not start the work,
it will just use the remaining memory.
As soon as it is not enough, GC priority gets higher, and it may even
suspend your program if necessary, to clean the memory.

Hope that helps.
 
C

CroDude

Thanks for a reply guys!

I've solved the problem and nocked myself a few times for a stupidity
how a jpeg could eat 25MB. Heh ... didn't think at all when posted this
question.
Anyway, memory filling is solved too by calling this after loading an image
to the picBox:

System.GC.Collect();
System.GC.WaitForPendingFinalizers();

Everything works now well ... memory is freeing fine and in a short amount
of time after each image load.
It seems that garbage collector is really slow when working with large
objects, and this method forces GC to go in a trash sweep.

Thanks again!

D ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top