Groups | Blog | Home
all groups > asp.net caching > april 2005 >

asp.net caching : Objects not Destroying


Scott M.
4/29/2005 12:00:00 AM
The browser has absolutely nothing to do with it. The server has no way of
knowing when a browser closes. In fact, by the time the browser even gets
the page, the server has already finished its processing and has
disconnected from the client.

All of your object variables will fall out of scope when the page finishes
its server-side processing. The objects themselves will remain in memory
until the Garbage Collector cleans up the heap.

Your problem sounds more like your are caching some results somewhere and
the server is reusing those results for new page requests.


[quoted text, click to view]

Richard
4/29/2005 1:36:02 PM
I'm running into a problem with objects not destroying when the browser
closes and is reopened. I have a custom collection that is declared public in
a module, and instantiated elsewhere (linkbutton click event). The collection
works fine, but when I close the browser and then I restart the web app, the
collection is still instantiated and contains alll of the items it had before
the browser was closed!

I was under the impression that once the browser closes, all objects go with
it. What I've been doing to "fix" this is in the main.aspx's pageload event,
checking if the object is not nothing, and clearing it if it is. This is not
limited to custom collections; I have a sortedlist class that is also still
"active" once the browser closes and then is restarted:

If Itinerary Is Nothing Then
Itinerary = New SortedList
Else
Itinerary.Clear()
Itinerary = Nothing
Itinerary = New SortedList
End If

Other than the above method, how can I ensure that my objects die when the
user clicks on the close button on the browser?

Thank you,
Richard
4/29/2005 3:16:08 PM
Hi Scott, thanks for your reply. I'm not caching the sortedlist such as in a
session object, so how can it be reused by the server?

It's declared in a module:
Module mdlGeneral

Public Itinerary As SortedList

End Module

Then it is instantiated and used in a click event:
Private Sub btnAddToItinerary_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnAddToItinerary.Click

'Create the object if necessary.
If Itinerary Is Nothing Then
Itinerary = New SortedList
End If

'Look for the issue id first. Add it if not already in the sortedlist.
If
Itinerary.ContainsKey(Session("ServiceIssueDetailIssueID").ToString) = False
Then
Itinerary.Add(Session("ServiceIssueDetailIssueID").ToString,
Session("ServiceIssueDetailIssueDescription").ToString)
End If

End Sub

[quoted text, click to view]
Scott M.
4/29/2005 7:02:57 PM
Why are you declaring it in a module? That's your problem. Itinerary is
never going to be nothing as long as the web application is running. Just
declare and instantiate it in the web page. If you need to persist it
between different pages consider caching it or passing it between pages.


[quoted text, click to view]

AddThis Social Bookmark Button