3 Replies - 4479 Views - Last Post: 07 September 2009 - 09:00 PM Rate Topic: -----

#1 zschallz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-September 09

PictureBox excessive memory usage in slideshow

Post icon  Posted 07 September 2009 - 08:22 PM

Hello. I've been tasked with making a slide show from a list of images in an XML file. I've implemented it by taking a picture box and displaying the listed images one by one and changing it with a timer. Unfortunately doing this causes my program to use quite a bit of memory. (It shoots up from ~10mb to about 100-200mb when the slide show is playing) It seems that when the picture changes the memory usage changes.

What could cause this? Does the picture box store the image (3mb images that are about 10 megapixels) in memory uncompressed? I can't seem to find much information about it on Google, but I am probably looking for the wrong things.

Here's the slide show part of the code, annotated.

		private int curPicture = 0; //index of current picture in xml file
		private int numPictures = managePictures.numberOfPictures(); //get the total number of pictures from xml file

		private void timer1_Tick(object sender, EventArgs e)
		{
			string[,] path = new string[1,1];

			while (true) // this will break when a "visible" image is found.
			{
				path = managePictures.outputPaths(curPicture, 1); // output the path and visibility setting of one picture from the xml file
				if (curPicture < numPictures-1) // make sure that we don't try to display pictures that don't exist
					curPicture++;
				else
					curPicture = 0;

				if(Boolean.Parse(path[0, 1]) == true) // check if picture is supposed to be shown
				{
					pictureBox1.ImageLocation = path[0, 0]; // display image
					break;
				}
			}
		}



Is there anything I can be doing better to manage my application's memory usage?

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: PictureBox excessive memory usage in slideshow

#2 zschallz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-September 09

Re: PictureBox excessive memory usage in slideshow

Posted 07 September 2009 - 08:45 PM

Small update... After calling GC.Collect(); every time I load an image memory use has went down from 200-300mb to a steady 100mb. It still feels like it's using an inordinate amount of memory though. Before the slideshow is started (before a picture is loaded) the application uses 15mb of ram. Even if the images are uncompressed, their file size would be around 30-40mb. Is there any thing else I can do to manage memory better?
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: PictureBox excessive memory usage in slideshow

Posted 07 September 2009 - 08:48 PM

Try using the line pictureBox1.Image.Dispose(); just before you set the imagelocation property. I think you are experiencing the memory leak because you are not properly disposing of the previous picture and releasing the memory before you load in the next image. See if that solves your problem.

:)
Was This Post Helpful? 1
  • +
  • -

#4 zschallz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-September 09

Re: PictureBox excessive memory usage in slideshow

Posted 07 September 2009 - 09:00 PM

View PostMartyr2, on 7 Sep, 2009 - 07:48 PM, said:

Try using the line pictureBox1.Image.Dispose(); just before you set the imagelocation property. I think you are experiencing the memory leak because you are not properly disposing of the previous picture and releasing the memory before you load in the next image. See if that solves your problem.

:)


Awesome! That brought my application down to 50mb, which seems in line with the 35mb uncompressed bitmap and the 15mb it used already.

In case anyone tries this again in the future, be sure if you use pictureBox.Image.Dispose() that you don't use pictureBox.ImageLocation like I did. Instead, use Picturebox.Image = Image.FromFile(path); Otherwise, your images won't show up.

Here is the updated code, in case it's of use to anyone.

		
		private void timer1_Tick(object sender, EventArgs e)
		{
			string[,] path = new string[1,1];

			while (true) // this will break when a "visible" image is found.
			{
				path = managePictures.outputPaths(curPicture, 1); // output the path and visibility setting of one picture from the xml file
				if (curPicture < numPictures-1) // make sure that we don't try to display pictures that don't exist
					curPicture++;
				else
					curPicture = 0;

				if(Boolean.Parse(path[0, 1]) == true) // check if picture is supposed to be shown
				{
					pictureBox1.Image.Dispose();
					pictureBox1.Image = Image.FromFile(path[0, 0]); // display image
					GC.Collect();
					break;
				}
			}
		}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1