disposing elements in asp pages

  • Thread starter Thread starter Mariusz
  • Start date Start date
M

Mariusz

Hello,
I have question regarding disposing objects in aspx/cs pages. If I create
some objects/Arrays/ListArrays in pageLoad do I have to dispose them (by
assigning the null value for objects that doesn't have dispose method). If
so then where should I do this? I need to use those objects in scripts in
aspx page (they are created in cs PageLoad) - so I have to dispose them
after they are not needed any more.

Cheers
Mariusz
 
Mariusz,

Apparently you are talking about page class members as opposed to Page_Load
local variables. Declarations you put in the aspx file also become page
class members. And as such they all get disposed when the page is disposed.
DotNet has build-in garbage collection and you don't have to care about it
except special cases, like database connections, when it is recommended to
dispose the objects explicitly.

Eliyahu
 
Hello,

Yes I'm talking about page class members. And I also know that declaratons I
put in aspx also became class members (I think that this is morte visible in
ASP.Net2 where class connected with aspx is declared as partial type). I
wasn't sure is all members will be disposed after the page will be disposed.
And what about multiple assigments to Arrays? Do I have to set it to null
before new assigment or not?

Cheers
Mariusz
 
Mariusz said:
Hello,

Yes I'm talking about page class members. And I also know that
declaratons I put in aspx also became class members (I think that
this is morte visible in ASP.Net2 where class connected with aspx is
declared as partial type). I wasn't sure is all members will be
disposed after the page will be disposed. And what about multiple
assigments to Arrays? Do I have to set it to null before new
assigment or not?
Cheers
Mariusz

When you set a variable to null, you don't dispose the object, you just
break the link between the variable and the object-data on the heap.
Setting the variable to some other value (without first setting it to null)
has the same effect.
Sometime later the garbage-collector removes the data from the heap
(if no more references exist).

I have even heard that the compiler knows (only in release-mode) when you
last use that variable, so that it can mark the object for CG before the variable
goes out of scope.

Hans Kesting
 
I'v always thought that setting value to null means that it can be garbage
collected.
Cheers
Mariusz
 
I'v always thought that setting value to null means that it can be garbage
collected.

Incorrect. Objects become eligible for garbage collection when there are no
remaining references to them from other objects that are in use. If, for
example, you create an instance of a class within another class, such as a
Page, for example, the Page has a reference to the class in it. When the
Page is unloaded, the class has no more references to it, and is therefore,
available for Garbage Collection. Setting a variable to null does nothing to
the class it references, other than removing the reference to the class
coming from the variable. If the class is referenced by some other in-memory
object, it remains until all references are gone.

In order to understand this, it is important to understand the difference
between a variable (or field) and a class. A variable is a "handle," if you
will, to whatever it contains. For example:

object s; // Empty variable, points to nothing
s = SomeObject; // Assigns the object SomeObject to variable
s.
s = AnotherObject; // Assigns AnotherObject to variable s.

When SomeObject is assigned to s, s IS NOT SomeObject, but a "handle" that
enables you to use SomeObject via s.
When AnotherObject is assigned to s, it is no longer referencing SomeObject.
Where is SomeObject? Still in memory, but referenced by no other objects. It
is therefore available to GC.

If s is in a function, when that function is pulled off the stack, s is
pulled with it, and whatever object it is referencing is no longer bound to
anything, and is therefore, available to CG.

If s is in a class instance, when that class instance is de-referenced, s is
pulled off the stack with the class instance, and whatever object it is
referencing is no longer bound to anything, and is therefore, available to
GC.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
Hi,
I should be more accurate next time :) I know everything You wrote in Your
mail. I meant that setting Your variable to null while there is no other
reference to object previously referenced by veriable sets this object
available for GC.
Sorry for misunderstanding.

Cheers
Mariusz
 
Back
Top