473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

disposing elements in asp pages

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
7 1128
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
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
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
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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris Reigrut | last post by:
I have a bunch of (old) pages that are outline-driven, and use the h1-h6 elements to define headings. Is there any way that I can create a stylesheet to show them in an indented style? For...
3
by: Phil Sherman | last post by:
What is the relationship between the snapshot elements: Log pages written Number write log IOs Number partial page log IOs There doesn't appear to be any information in my PDF System monitor...
10
by: Patrick De Ridder | last post by:
I have been looking at an example, and there is something I don't inderstand. Given: form1 calls form2 --------- Question: What is the use of having these lines in form2 --------------...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
4
by: Dotcom | last post by:
I have an ASP.NET application that is mysteriously acquiring height and width attributes on a particular IMG element on multiple pages. This is not being caused by someone editing or uploading new...
5
by: Chris | last post by:
I have a form that requires drawing custom lines on it. The color of the lines is suppose to be the same as the forcolor of the form. Am I doing this the most efficent and correct way? ...
8
by: dd | last post by:
Has anyone found a way around the problem IE has if you create elements (script or div, doesn't seem to matter) BEFORE the document.readyState is "complete" ? I know you can sometimes get away...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.