By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,966 Members | 812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,966 IT Pros & Developers. It's quick & easy.

disposing elements in asp pages

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

"Mariusz" <ma**@post.pl> wrote in message news:41******@news.home.net.pl...
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

Nov 18 '05 #2

P: n/a
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
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.

Nov 18 '05 #3

P: n/a
Mariusz wrote:
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
Nov 18 '05 #4

P: n/a
Don't bother. Garbage collection takes care about everything.

Eliyahu

"Mariusz" <ma**@post.pl> wrote in message news:41******@news.home.net.pl...
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
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.


Nov 18 '05 #5

P: n/a
I'v always thought that setting value to null means that it can be garbage
collected.
Cheers
Mariusz

Użytkownik "Hans Kesting" <ne***********@spamgourmet.com> napisał w
wiadomości news:%2****************@TK2MSFTNGP14.phx.gbl...
Mariusz wrote:
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

Nov 18 '05 #6

P: n/a
> 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
"Mariusz" <ma**@post.pl> wrote in message
news:41********@news.home.net.pl... I'v always thought that setting value to null means that it can be garbage
collected.
Cheers
Mariusz

Użytkownik "Hans Kesting" <ne***********@spamgourmet.com> napisał w
wiadomości news:%2****************@TK2MSFTNGP14.phx.gbl...
Mariusz wrote:
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


Nov 18 '05 #7

P: n/a
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

Użytkownik "Kevin Spencer" <ks******@takempis.com> napisał w wiadomości
news:e6**************@TK2MSFTNGP12.phx.gbl...
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
"Mariusz" <ma**@post.pl> wrote in message
news:41********@news.home.net.pl...
I'v always thought that setting value to null means that it can be
garbage
collected.
Cheers
Mariusz

Użytkownik "Hans Kesting" <ne***********@spamgourmet.com> napisał w
wiadomości news:%2****************@TK2MSFTNGP14.phx.gbl...
> Mariusz wrote:
>> 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
>
>



Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.