Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

How to use Dispose?

  Asked By: Nancy    Date: Apr 06    Category: Asp.net    Views: 1101
  

I am just wondering how to code Dispose. OK, I check for each control and
database objects used in the code for not being nulls and if so, I dispose
them individually and setting them to null. Not sure if this is correct. But
I am not clear from where to call this method from.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Mae Roberts     Answered On: Apr 06

You can call  Dispose() method  when you stop using certain instance of an object.
But Garbage collector will dispose resources as needed, so Dispose itself
doesn't actually free memory and other resources...

 
Answer #2    Answered By: Freda Lane     Answered On: Apr 06

Ok, I understand that but on a web page, when it is guided by user
events/activities, how do I know that when the user would close the browser
before which I could dispose objects  like datagrid. I may dispose Connection
objects etc. but I can not call  dispose for all the objects .

 
Answer #3    Answered By: Hooriya Khan     Answered On: Apr 06

You have to know how ASP.NET works. On everypostback the application recreates
complete Page's control  tree. If any event handler creates something more, even
that is created. After the Response is sent to the user, all resources are
released (as disposed to Garbage Collector -GC). If between your postback
someone else (another user session) needs more memory, garbage collector will
fisically free your resources and by your postback they would have to be
recreated a new. Otherwise they'll be pulled of the memory.

What I wanted to say is that every visit to the page regreates all the controls
to the memory (totaly fresh or not disposed from the GC). So You don't have to
worry so much about users that close their windows, because their resources are
already in the hands of GC. They will be relesed from the memory as soon as the
server will need more memory resources.

It's different with database connections which are a "time consuming" objects.
It's normal to close a connection because it communicates with another app (or
even computer), but the connection object is also disposed to GC and released by
GC when needed.

The only thing to remember is that ASP.NET application creates all the objects
on every request to any page included in the application.
When there are no request, there are also no object instances. But memory can be
"occupied" but is marked as "freeable" by the GC.

 
Didn't find what you were looking for? Find more on How to use Dispose? Or get search suggestion and latest updates.




Tagged: