Problems with pictureboxes and memory usage

J

Jeff

I've been working on an application for a while now that has been giving
me some trouble when it comes to working with a picturebox and memory
usage. My company deals with digital imaging, so we are dealing with
high resolution images (2500x3300pixels.)

The application allows the user to browse through the images in a
directory to find the images they need (files listed in a listbox,
clicking on the item displays it in a picturebox.) The problem is that
memory usage just keeps climbing and climbing as the user browses
through the images.

The program will start around with about 24MB of usage. But after
viewing about a dozen images, it will have doubled to around 50MB. This
number will just keep on climbing as more images are viewed.

I've tried loading the image two ways
1.
picImage.Image = Image.FromFile(filename)
and the second way which I saw a post about using to resolve memory leaks
2.
Dim _image As Image
_image = Image.FromFile(filename)
picImage.Image = _image

They both end up with the same results. The only way I have found that
keeps the memory from skyrocketing is to call GC.Collect() at the end of
the function that loads the images, which is supposed to be one of the
most evil things you can do in most situations.

Anyone have any ideas? I don't want to have to call GC.Collect() since
everyone says how horrible it is, but it's the only reliable way I've
found to keep the memory down (the machines running the application will
be Win98 machines with only 256MB of RAM for a while at least, so I'd
like to keep the memory usage down.)

Thanks,
Jeff
 
H

Herfried K. Wagner [MVP]

Jeff,

Jeff said:
I've been working on an application for a while now that has been giving
me some trouble when it comes to working with a picturebox and memory
usage. My company deals with digital imaging, so we are dealing with high
resolution images (2500x3300pixels.)

The application allows the user to browse through the images in a
directory to find the images they need (files listed in a listbox,
clicking on the item displays it in a picturebox.) The problem is that
memory usage just keeps climbing and climbing as the user browses through
the images.

The program will start around with about 24MB of usage. But after viewing
about a dozen images, it will have doubled to around 50MB. This number
will just keep on climbing as more images are viewed.

The memory usage shown in the task manager is not significant. An
application may have a much smaller memory requirement than shown in the
task manager because the GC internally "frees" memory and later reuses it.
I've tried loading the image two ways
1.
picImage.Image = Image.FromFile(filename)
and the second way which I saw a post about using to resolve memory leaks
2.
Dim _image As Image
_image = Image.FromFile(filename)
picImage.Image = _image

They both end up with the same results.

The code is semantically identical, so you won't experience a difference.
The only way I have found that keeps the memory from skyrocketing is to
call GC.Collect() at the end of the function that loads the images, which
is supposed to be one of the most evil things you can do in most
situations.

The GC should run from time to time automatically, so there is no need to
explicitly call it in most situations.
 
B

Bob Powell [MVP]

If your code is simply replacing the image in the picturebox and not
explicitly calling Dispose on the images then you'll probably have problems.

Try something like:

Image i=myPicBox.Image;
myPicBox.Image=newimage;
i.Dispose();

--
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.
 
J

Jeff

Thanks Bob! That did the trick. I had been trying to dispose, but was
getting errors instead. I would have never thought of doing it that
way. The memory usage is now the same as when I was using GC.Collect.
Perfect timing too, as I just finished the application otherwise.
Thanks again.

Jeff
 

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

Similar Threads


Top