By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,712 Members | 1,579 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

Problems with pictureboxes and memory usage

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jeff,

"Jeff" <js******@woh.rr.com> schrieb im:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

P: n/a
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.

"Jeff" <js******@woh.rr.com> wrote in message
news:uS**************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #3

P: n/a
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

Bob Powell [MVP] wrote:
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();

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.