Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory Not Released

Status
Not open for further replies.

MJV57

Programmer
Apr 18, 2009
87
CA
I have created an application in c# but I find even though I close a form that brought in alot of data to local memory that memory is not released when I close the form. Is there something I can do for this?
 
sure there is. now what that is we cannot say without seeing the code.

I would recommend researching the purpose/concepts of the IDisposable interface and GC Garbage Collection.

if you have code like this in your system
Code:
var cnn = new SqlConnection(...);
var cmd = new SqlCommand(cnn);
cnn.Open();
//set sql string and execute query.
cnn.Close();
then this is the first place to start. You are not disposing of your connections/commands. update the code to look something like this instead
Code:
using(var cnn = new SqlConnection(...))
{
   cnn.Open();
   using(var cmd = new SqlCommand(cnn))
   {
      //set sql string and execute query.
   }
}
now the resources will be disposed of when the block is existed. there are additional benefits to this, which you'll pick up reading about IDisposable.
also note that with db connections i do not need to explicitly call cnn.Close();

also if you wire up event manually in code. you need to unwire them as well, otherwise a reference to the handler is lingering around and CG will not clean up because it assumes the handler is still needed.
Code:
class MyForm : Form
{
   public MyForm()
   {
       Load += MyForm_Load;
   }

   private void MyForm_Load(object sender, EventArgs e)
   {
       //do something when the form loads
   }

   protected override void Dispose()
   {
       Load -= MyForm_Load;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I have used the following on loading the form. What do I do in this case?


private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'estimatorDataSet.QuoteCalcBody1' table. You can move, or remove it, as needed.
this.quoteCalcBody1TableAdapter.Fill(this.estimatorDataSet.QuoteCalcBody1);
// TODO: This line of code loads data into the 'estimatorDataSet.QuoteCalcHeader1' table. You can move, or remove it, as needed.
this.quoteCalcHeader1TableAdapter.Fill(this.estimatorDataSet.QuoteCalcHeader1);
// TODO: This line of code loads data into the 'estimatorDataSet.QuoteCalcHeader' table. You can move, or remove it, as needed.
this.quoteCalcHeader1TableAdapter.Fill(this.estimatorDataSet.QuoteCalcHeader1);
// TODO: This line of code loads data into the 'estimatorDataSet.QuoteCalcBody' table. You can move, or remove it, as needed.
this.quoteCalcBody1TableAdapter.Fill(this.estimatorDataSet.QuoteCalcBody1);

}
 
nothing that i can see. I'm not aware of the mechanics of table adapters.

The more "code" you create using drag/drop controls, the less control you have.

I would also think there would be more code in your application than these 4 lines.

you could profile the memory usage of the application using a tool like dotTrace.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
It's also possible that the memory "loss" you're seeing is the .NET runtime assemblies remaining in memory.

Chip H.


____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top