Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not using any GDI objects in my code to dispose that leads to managed memory leak.
Attaching my code, can anyone tell me why that memory is getting piled up.

C#
namespace SampleTemplate
{
    public partial class SelectTemplate : Form
    {
        public SelectTemplate()
        {
            InitializeComponent();
        }

        private void SelectTemplate_Load(object sender, EventArgs e)
        {   
            //
            // Set the Timer interval
            //
            this.forwardTimer.Interval = 3000;

            //
            // Count the number of Images in the Folder specified
            //
            try
            {
                string dir = "C:\\SignatureTemplates";
                files = Directory.GetFiles(dir, "*.jpg");

                numberOfImages = files.Length;
                backwardCount = numberOfImages;

                //
                // Load the first Image in to the picturebox
                //
                if (0 != currentCount)
                {
                    image = Image.FromFile(files[currentCount]);
                } // if
                else
                {
                    image = Image.FromFile(files[0]);
                } // else
                this.pictureBox1.Image = image;
                this.pictureBox1.SizeMode = PictureBoxSizeMode.Normal;

                //
                // Display the count of the Template
                //
                this.countLabel.Text = (currentCount + 1) + "/" + numberOfImages;
            } // try
            catch (Exception)
            {
                MessageBox.Show("Exception caught");
            }
            finally
            {
                if (0 == numberOfImages)
                {
                    this.Close();
                }
            } // finally

            //this.pauseBtn.Enabled = false;

        } // SelectTemplate_Load

        private void pauseBtn_Click(object sender, EventArgs e)
        {
            //
            // Stop the timer 
            //
            this.forwardTimer.Stop();
            playFlag = false;
        }

        private void playBtn_Click(object sender, EventArgs e)
        {
            //
            // Start the timer 
            //
            this.forwardTimer.Start();
            playFlag = true;
        }

        private void forwardTimer_Tick(object sender, EventArgs e)
        {
            //
            // operation to be performed after the specified time elapses.
            //
            if (currentCount < numberOfImages - 1)
            {
                this.pictureBox1.Image = Image.FromFile(files[currentCount + 1]);
                currentCount++;
                this.countLabel.Text = (currentCount + 1) + "/" + numberOfImages;
            } // if
            else if (numberOfImages - 1 == currentCount )
            {
                currentCount = 0;
                this.pictureBox1.Image = Image.FromFile(files[currentCount]);
                this.countLabel.Text = (currentCount + 1) + "/" + numberOfImages;
            } // else if
        }
Posted
Updated 6-Nov-15 9:43am
v2

Before
this.pictureBox1.Image = Image.FromFile(files[currentCount]);
this.pictureBox1.Image = Image.FromFile(files[currentCount + 1]);

You Need to Do this :
if(this.pictureBox1.Image  != null)
{
this.pictureBox1.Image.Dispose();
this.pictureBox1.Image  = null;
}

you need to dispose the old image before assign new image in your timer tick.

Also For :
this.countLabel.Text = (currentCount + 1) + "/" + numberOfImages;

you better make it this way :
this.countLabel.Text = string.Format("{0}/{1}",(currentCount + 1) ,numberOfImages);

cause using + with strings will create new string to contact

Edit :
well this is the only leaks I can find on your posted code, memory leaks is tricky and hard to catch if you don't have the right tools.

For start don't use Windows Task Manager to detect leaks because it's not accurate instead use Memory Profiler program to help you.

Here some useful links to help you with your search :

How to detect and avoid memory and resources leaks in .NET applications[^]

Garbage Collector Basics and Performance Hints[^]

How to measure the total memory consumption of the current process programatically in .NET?[^]

8 Most common mistakes C# developers make
[^]
 
Share this answer
 
v4
Comments
Reddy1111 6-Nov-15 4:34am    
even this is not working, i have checked it with preformance monitor and the private bytes are increasing when the timer is on.
Instead of creating an array of filenames at the start, why not create an array of images. Then you just need to assign the correct image. No file IO, Disposing, or memory allocation require.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900