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

Started by
6 comments, last by davepermen 16 years, 7 months ago
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();
        }


~Bolt"All men dream: but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity: but the dreamers of the day are dangerous men, for they may act their dreams with open eyes, to make it possible." This I did...
Advertisement
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();
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 :(
~Bolt"All men dream: but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity: but the dreamers of the day are dangerous men, for they may act their dreams with open eyes, to make it possible." This I did...
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.
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...
~Bolt"All men dream: but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity: but the dreamers of the day are dangerous men, for they may act their dreams with open eyes, to make it possible." This I did...
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.
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!!

~Bolt"All men dream: but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity: but the dreamers of the day are dangerous men, for they may act their dreams with open eyes, to make it possible." This I did...
haha, this is cool. though i hate the gdi wrapper for these situations (i had similar ones..).

definitely sometimes hard to debug..
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

This topic is closed to new replies.

Advertisement