Igglyboo
Igglyboo

Reputation: 845

Closed form will not release memory

I have a medium sized application that containers two forms. When i open the application it goes to the first form and it consumes about 17mb. Then I open the second form and close the first form, about 57mb is being consumed. I closed the second and re open the first, 33mb. Close the first and reopen the second, 66mb. Why is this memory not being reclaimed? Here is the code I use to close the current form and open the new form.

private void honButton_Click(object sender, EventArgs e)
{
    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));

    this.Close();
    this.Dispose();

    t.Start();
}


 public static void ThreadProc()
 {
    Application.Run(new Form1());
 }

Upvotes: 2

Views: 1442

Answers (2)

Kashif
Kashif

Reputation: 14430

Are you using any profiling tool to check you memory usage or Windows Task Manager. Task manager is not good place to check memory usage of your application. Use perfmon's Process metrics for things like Private Bytes.

Upvotes: 0

Eric J.
Eric J.

Reputation: 150108

If memory is not being reclaimed, it's probably because the garbage collector did not choose to collect it yet.

GC is not deterministic in .NET.

In fact, I have written apps that aggressively allocate memory (process very large data structures) which do not begin releasing memory until the available virtual address space for the process is nearly exhausted).

When GC runs will depend on which garbage collector you are using (server vs. workstation), which .NET implementation, and on the memory usage profile of your application.

Upvotes: 1

Related Questions