Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is memory released on Form.close()?

I am working on feedback application which have lots of form open and close operation. I noticed few changes in memory changes in my application when i start my application it takes 25 MB. With each feedback user give it increases 3 MB in memory usage. On every form i had used this.close() when it jumps from one to other or there is any close operation. What can be the possible reason of memory increases.

Do i need to call garbage collector manually, as everyone says its not good practice.

In this i am using dual monitor scenario in which application take snapshot of secondary screen after each 500 ms and shows it on primary screen. For this I am using the code shown below:

public EntryForm()
{
    sc = Screen.AllScreens;
    dbDms = new HondaDb(UtilityFunctions.getServerConnection());
    db = new HondaDb(UtilityFunctions.getClientConnection());
    bmpScreenshot = new Bitmap(sc[1].Bounds.Width,
                       sc[1].Bounds.Height,
                       PixelFormat.Format32bppArgb);

     Create a graphics object from the bitmap.
    gfxScreenshot = Graphics.FromImage(bmpScreenshot);
    Timer timerClientScreen = new Timer();
    timerClientScreen.Interval = 500;
    timerClientScreen.Enabled = false;
    timerClientScreen.Start();
     timerClientScreen.Tick += new EventHandler(timer_TickClient);
}

void timer_TickClient(object sender, EventArgs e)
{

    // Take the screenshot from the upper left corner to the right bottom corner.
    gfxScreenshot.CopyFromScreen(sc[1].Bounds.X, sc[1].Bounds.Y,
                                0, 0, sc[1].Bounds.Size, CopyPixelOperation.SourceCopy);
    // Save the screenshot to the specified path that the user has chosen.
    pictureBoxClient.Image = bmpScreenshot;
}

For closing of form on open of other I am using the code below

formOpen.show();
formClose.Close();

Suggest me how can I save memory usage.

like image 816
Hot Cool Stud Avatar asked Jan 09 '23 03:01

Hot Cool Stud


2 Answers

It does, but just your UI objects. It isn't automatic for the variables you use. In an app like this, using big objects that take very little GC heap space but lots of unmanaged resources, the garbage collector doesn't typically run often enough to keep you out of trouble. You have to help and explicitly dispose the objects so you don't leave it up to the GC to take care of the job.

It may take too long for it to start running, you can build up a lot of unmanaged memory usage before it gets to run the finalizers. Potentially crashing your program with OOM, although you are still very far removed from that problem. Right now you are just running "heavy".

Add an event handler for the FormClosed event. You need to call the Dispose() method on the gfxScreenshot and bmpScreenshot objects. And surely those HondaDb objects need some kind of cleanup as well.

Do not assume that will instantly solve memory usage increments, the GC is not eager to release address space back to the operating system. Keeping it around instead with the assumption that you are likely going to have a need for it soon. The proper usage pattern is it stabilizing after a while at a reasonable number, then suddenly dropping and building back up. A saw-tooth pattern. Write a little unit test that calls creates and destroys your form object repeatedly, ensuring that it does the non-trivial jobs of taking the screenshot and accessing the dbase. Now you know with confidence that you don't have a run-away leak problem.

like image 145
Hans Passant Avatar answered Jan 11 '23 22:01

Hans Passant


No, when you call Form.Close() you are just telling to close the form. the object is still there in memory and if you have a reference to it, it will be there until you have held that reference.

.NET has automatic garbage collection mechanism which collect objects that are garbage (you have no reference to them and they can not be accessed). So objects are removed from memory when they become garbage and .NET garbage collector starts it works. You can force executing garbage collector by calling GC.Collect().

More about GC

like image 31
Hamid Pourjam Avatar answered Jan 11 '23 22:01

Hamid Pourjam