Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

So I was reading that you should make sure to dispose any DataSet or DataView object on the Page_Unload event since they tie up resources and they won't be disposed otherwise - it will have to wait until GC gets around to Finalizing the object.

 

Simple enough - makes since until:

 

If you keep the DataView or DataSet in Session so you don't have to go through your data access classes and hit the database each time, you now have a potential problem. When you put a DataView or DataSet in Session it's put by reference (it's an object). So if you have:

 

In the fields:

WithEvents ds as DataSet

 

In a function somewhere:

SomeDataAdapter.Fills(ds)

Session("Whatever") = ds

ds.Tables.Clear()

 

And in a later function:

 

Dim dt as DataTable = DirectCast(Session("Whatever"), DataSet).Tables(0)

 

You will get a null reference.

 

So.....

 

If you dispose a DataSet or DataView that you have as an object on your page that you may set from Session or ViewState after initial load, and then in your Page_Unload event you're trying to Dispose that object, which because of the reference be trying to dispose an object in Session... which for one, can that even be done, and two, would that cause GC to re-register it for Finalize, and three if that occurs now it would live in Session until Session died because it's not in the fReachable queue (able to be finalized because it's not tied to any other objects).

 

Am I correct in all of this or am I way off?

  • Administrators
Posted

The Dispose method found in DataSets and DataTables is actually due to it inheriting from Component, calling Dispose on a DataTable or DataSet doesn't actually free up the table(s) at all. Also neither of them provide their own finalizer so there is no need to worry about needing to re-register etc.

If you need the data to be available then store it in a Session, Application or Cache variable, when it is no longer required clear the Session, Application or Cache variable and let the GC collect it at it's leisure.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...