how to free memory?

  • Thread starter Thread starter Swaroop
  • Start date Start date
S

Swaroop

In .NET Compact Framework, Can I free memory explicitly without waiting
for garbage collector to frr the memory?
 
The recommendations I've seen are these:

1. Call Dispose() methods on objects / forms when you can. In
particular, when you use .ShowDialog(), don't forget to call .Dispose()
at some point afterward. This may, in turn, require that you have your
objects implement IDisposable (if they have members that implement
IDisposable).

2. Call the garbage collector explicitly using GC.Collect() after you
finish with a large amount of memory that you've been holding for some
time. The garbage collector uses statistical methods to determine when
to collect and what to collect, which makes it difficult for it to
recognize sudden changes in objects' use of memory. The GC is very good
at collecting the kinds of objects that pop into existence, get used
for a bit, then disappear. It's not very good at handling large amounts
of memory that are gradually allocated, held for a long time, then
suddenly freed. Calling GC.Collect() at judicious points in your
application can help with that.
 
First of all, Thanks...
I used GC.Collect(). But it is not freeing the memory.
Actually I am working for Pocket PC and for a specific search criteria,
I get 11000 records and 10MB memory is allocated for it. While closing
the form, I called GC.Collect(), but it did not free the single byte.
As Pocket PC is having less memory, I need to free the above memory for
other forms and data.
Any solution???
 
You should use a dispose pattern in your form. Most probably there are
references that prevent the GC to collect your objects.
 
swaroop,

The Dispose will not release memory.

The GC will release memory for every object that has no reference anymore
itself or has not any reference anymore to it. (If that is fulfilled than
you can force the GC, however how you tell your problem I would first look
at the objects that you have declared globaly or whatever, because those
will without setting references to null only be released at the end of the
program).

Be aware that if you have a reference by instance to a by the designer
created form or component, there is no need to dispose the used controls or
whatever from it. The Idisposable method iomplemented in that form and
component are for that.

In this are some unluckily some not documented methods for what you can
better call dispose yourself, in those are bitmaps and the already by Bruce
mentioned Showdialog.

I hope this helps,

Cor
 
I am getting data (11000 records) through data table and after I load
that data in the list, I clear the data table object and set it to
null. But the form remains in memory. I tried by closing the form also,
but it also did not free the memory.
 
Swaroop.

In extention to my previous answer. If you do something as

mylist.Datasource = mytable;

Than that mytable will exist as long as that datasource reference exist.

Cor
 
Thanks Cor
Then while closing the form, if I set
myList.DataSource = null
Will it free the memory, when form still remains in memory??
Or do I need to do both things, close the form and make the data source
NULL?
 
Swaroop,

I assume that setting the datasource to Null will do the job. (Assuming that
that is the last reference to your table)

I hope this helps,

Cor
 
Be aware that DataTable has a Dispose method. If you're programming in
a tight memory environment, you should call Dispose() whenever you're
through with an object of a class publishes that method.

If you use the DataTable as a local variable, call Dispose() on it just
before it goes out of scope. If you have it as a class member in your
Form class, call Dispose() on the DataTable in your form's Dispose()
method.

You should check the documentation for all of the classes that you use,
both as local variables and as your own class's members, and call
Dispose() for those objects that offer the possibility.

Also, watch out for event subscriptions, particularly subscriptions to
static events. If your form subscribes to any events you should
unsubscribe in the form's Dispose method. If any event publishers still
have methods from your form on their event lists, the form will not be
free for garbage collection.

Even after all of this, you can't force the GC to collect the memory.
However, it should normally do so when you call GC.Collect(). If the
memory stays around, it's normally because you are still holding a
reference to the form somewhere. This is another advantage of Dispose:
if you call Dispose on an object that you _think_ you're not using any
more, and you try to use it, your program should crash when you try to
use the disposed object, or some disposed member within it. That's what
happens to me from time to time: I believe that I'm through with
something and I Dispose() it, but then I find some lingering event
subscriptions or something when my program dies with a "can't use
disposed object" error.
 
Thanks Bruce & Cor
I checked for the Dispose() method for data table
But in the intellisence, I did not find Dispose() method, thats why I m
setting data table object to null
In order to get Dispose() method for data table, do I need to include
any namespace or do I need to override the method?
Please explain this...

Thanks again
- Swaroop
 
Swaroop said:
Thanks Bruce & Cor
I checked for the Dispose() method for data table
But in the intellisence, I did not find Dispose() method, thats why I m
setting data table object to null
In order to get Dispose() method for data table, do I need to include
any namespace or do I need to override the method?
Please explain this...

You just need to cast it to IDisposable - it (or rather, some base
class I believe) implements IDiposable with explicit interface
implementation.
 
Jon,

In my opinion does dispose not release a datatable that is binded.

Try this sample if you want..

private void Form1_Load
(object sender, System.EventArgs e)
{
DataGrid dtg = new DataGrid();
DataTable dt = new DataTable();
dt.Dispose();
Controls.Add(dtg);
dt.Dispose();
dtg.Left = 10;
dtg.Top = 10;
dtg.Click += new System.EventHandler(this.DtgClick);
dt.Columns.Add();
dt.Dispose();
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0]= "Hello I am still here";
dt.Dispose();
dtg.DataSource = dt;
dt.Dispose();
}
private void DtgClick
(object sender, System.EventArgs e)
{
DataGrid dtg = (DataGrid)Controls[0];
DataTable dt = (DataTable)dtg.DataSource;
MessageBox.Show(dt.Rows[0][0].ToString());
}

Cor
 
Cor Ligthert said:
In my opinion does dispose not release a datatable that is binded.

I don't believe that DataTable.Dispose() actually does anything unless
other than remove the DataTable from its "site" - at least on the
normal framework. I was just explaining how you could actually run it
:)
 
Sorry all, my mistake.

It's not DataTable on which you should call Dispose(), but DataSet.

Again, sorry for the confusion.
 
Bruce Wood said:
Sorry all, my mistake.

It's not DataTable on which you should call Dispose(), but DataSet.

Again, sorry for the confusion.

Again though, I don't think DataSet.Dispose actually does anything
significant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top