Sign in to follow this  

[.net] Running out of memory when loading then deleting bitmaps!!?

This topic is 3741 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, if someone has ran into this and solved it please help... I think it may have to do with .net's Garbage Collector. Anyhow, here is the problem..I'm loading bitmaps to gather data on them and then I leave them for the GC to dispose of.. So I trimmed down my code to this simple test and i keep getting out of memory exceptions when I get to the 520th image. All this code does is load a bitmap from a file then disposes it and moves on the next one... any ideas?? PS: I call loadImageLibrary() from another function where it just iterates from 0-750, and calls loadImageLibrary(i).... of course it never makes it to 750, it errors out at 520.
private void loadImageLibrary(int imageNum)
        {

            fileProcessed.Text = "Loading image: " + (this.T_SourceImageTemplate.Text).Replace("*", imageNum.ToString());
            fileProcessed.Refresh();

            // see if the file exists
            if (!(File.Exists((this.T_SourceImageTemplate.Text).Replace("*", imageNum.ToString()))))
            {
                MessageBox.Show("File: " + (this.T_SourceImageTemplate.Text).Replace("*", imageNum.ToString()) + "is missing");
                checksComplete = false;
                return;
            }

            //Image tempImage = (Bitmap)Image.FromFile((this.T_SourceImageTemplate.Text).Replace("*", imageNum.ToString()));
            Bitmap currentImage = new Bitmap((Bitmap)Image.FromFile((this.T_SourceImageTemplate.Text).Replace("*", imageNum.ToString())), 
                                      new Size(imageLibWidth, imageLibHeight));
            currentImage.Dispose();
        }


Share this post


Link to post
Share on other sites
Try splitting the image resize into two steps so you can dispose of them separately.

Bitmap currentImage = (Bitmap) Bitmap.FromFile(this.T_SourceImageTemplate.Text.Replace("*", imageNum.ToString()));

Bitmap resizeImage = new Bitmap(currentImage, new Size(imageLibWidth, imageLibHeight));

resizeImage.Dispose();
currentImage.Dispose();

Share this post


Link to post
Share on other sites
Quote:
Original post by Headkaze
Try splitting the image resize into two steps so you can dispose of them separately.

Bitmap currentImage = (Bitmap) Bitmap.FromFile(this.T_SourceImageTemplate.Text.Replace("*", imageNum.ToString()));

Bitmap resizeImage = new Bitmap(currentImage, new Size(imageLibWidth, imageLibHeight));

resizeImage.Dispose();
currentImage.Dispose();


I tried that and unfortunately, I'm still getting the oom messages :(

Share this post


Link to post
Share on other sites
Hmmm.. maybe try

using (Bitmap currentImage = (Bitmap)Bitmap.FromFile(this.T_SourceImageTemplate.Text.Replace("*", imageNum.ToString())))
{
using (Bitmap resizeImage = new Bitmap(currentImage, new Size(imageLibWidth, imageLibHeight)))
{
// Do stuff
}
}


I believe this will force the GC to clean up straight away.

Share this post


Link to post
Share on other sites
I tried that too, but to no avail. I even tried delaying the loop and that didn't work either. It can't be memory, because im only loading a 1024x768 image, and then supposedly when I reuse the same variable it should merely fill out the existing memory, but somehow this is not happening...

Share this post


Link to post
Share on other sites
The out of memory error in GDI+ can mean many things, so it's hard to say.

Just as an experiment, instead of trying to load the 750 images try loading the first 300 3 times. This way you can see if image #520 is causing the problem. Also maybe try removing image 520 to see if it will make it past it. Maybe even try loading the same image 750 times.

Share this post


Link to post
Share on other sites
Quote:
Original post by Headkaze
The out of memory error in GDI+ can mean many things, so it's hard to say.

Just as an experiment, instead of trying to load the 750 images try loading the first 300 3 times. This way you can see if image #520 is causing the problem. Also maybe try removing image 520 to see if it will make it past it. Maybe even try loading the same image 750 times.


Thank you sir! I feel like a complete idiot. It WAS one of the image files..lol.. in fact I did a batch renaming of all my files in a certain folder and didn't pay attention to the fact that I had some PDF's in there!

Thanks!!

Share this post


Link to post
Share on other sites

This topic is 3741 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this