473,569 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instantiating a class in.NET

Hi,

I have a class in .Net which I instantiate in my code
behind on one of my aspx pages.

When I am finished with the instance, what is teh best
method of taking it out of memory. Should I set it to
nothing or can I dispose of it?

Thanks,
Carl.
Nov 17 '05 #1
9 1138
Set it equal to nothing.
You shouldn't need any fancy disposal stuff unless the class is holding open
unusually valuable resources.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Carl" <ca**@nospam.co m> wrote in message
news:92******** *************** *****@phx.gbl.. .
Hi,

I have a class in .Net which I instantiate in my code
behind on one of my aspx pages.

When I am finished with the instance, what is teh best
method of taking it out of memory. Should I set it to
nothing or can I dispose of it?

Thanks,
Carl.

Nov 17 '05 #2
True, but it never hurts to be tidy.
In VB6 you theoretically didn't need to clean things up either, but the
reality was that sometimes the automatic cleanup didn't work quite as well
as it claimed.
It seems to behave better in .NET but I'm still not sure if I trust it 100%.
Better to be safe than sorry and clean up your own messes.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Kevin Spencer" <ke***@takempis .com> wrote in message
news:%2******** *********@tk2ms ftngp13.phx.gbl ...
It isn't necessary to set it to nothing, nor does it have any effect to do
so (unlike COM). The object will be up for Garbage Collection as soon as it passes out of scope.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
http://www.takempis.com
Complex things are made up of
lots of simple things.

"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uW******** ******@TK2MSFTN GP11.phx.gbl...
Set it equal to nothing.
You shouldn't need any fancy disposal stuff unless the class is holding

open
unusually valuable resources.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Carl" <ca**@nospam.co m> wrote in message
news:92******** *************** *****@phx.gbl.. .
Hi,

I have a class in .Net which I instantiate in my code
behind on one of my aspx pages.

When I am finished with the instance, what is teh best
method of taking it out of memory. Should I set it to
nothing or can I dispose of it?

Thanks,
Carl.



Nov 17 '05 #3
Steve,

Why even bother setting the reference to Nothing? This isn't COM - it won't
decrement the reference count.

--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uW******** ******@TK2MSFTN GP11.phx.gbl...
Set it equal to nothing.
You shouldn't need any fancy disposal stuff unless the class is holding open unusually valuable resources.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Carl" <ca**@nospam.co m> wrote in message
news:92******** *************** *****@phx.gbl.. .
Hi,

I have a class in .Net which I instantiate in my code
behind on one of my aspx pages.

When I am finished with the instance, what is teh best
method of taking it out of memory. Should I set it to
nothing or can I dispose of it?

Thanks,
Carl.


Nov 17 '05 #4
My mother always taught me to clean up after myself.
Old habits die hard.
:-)

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"John Saunders" <jo***********@ surfcontrol.com > wrote in message
news:OZ******** ******@TK2MSFTN GP10.phx.gbl...
Steve,

Why even bother setting the reference to Nothing? This isn't COM - it won't decrement the reference count.

--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uW******** ******@TK2MSFTN GP11.phx.gbl...
Set it equal to nothing.
You shouldn't need any fancy disposal stuff unless the class is holding

open
unusually valuable resources.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Carl" <ca**@nospam.co m> wrote in message
news:92******** *************** *****@phx.gbl.. .
Hi,

I have a class in .Net which I instantiate in my code
behind on one of my aspx pages.

When I am finished with the instance, what is teh best
method of taking it out of memory. Should I set it to
nothing or can I dispose of it?

Thanks,
Carl.



Nov 17 '05 #5
Steve,

Do you clean paper plates before discarding them? Then why set a variable
to null when it's going to disappear as soon as you leave the scope? :-)

--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uq******** ******@tk2msftn gp13.phx.gbl...
My mother always taught me to clean up after myself.
Old habits die hard.
:-)

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"John Saunders" <jo***********@ surfcontrol.com > wrote in message
news:OZ******** ******@TK2MSFTN GP10.phx.gbl...
Steve,

Why even bother setting the reference to Nothing? This isn't COM - it

won't
decrement the reference count.

--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uW******** ******@TK2MSFTN GP11.phx.gbl...
Set it equal to nothing.
You shouldn't need any fancy disposal stuff unless the class is
holding open
unusually valuable resources.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Carl" <ca**@nospam.co m> wrote in message
news:92******** *************** *****@phx.gbl.. .
> Hi,
>
> I have a class in .Net which I instantiate in my code
> behind on one of my aspx pages.
>
> When I am finished with the instance, what is teh best
> method of taking it out of memory. Should I set it to
> nothing or can I dispose of it?
>
> Thanks,
> Carl.



Nov 17 '05 #6
I agree that it is not a step that is necessary.
But in general it's good practice to free memory as soon as you're done with
it.
By setting the object to nothing, you're permitting the garbage collector to
pick it up immediately if it so chooses, rather than forcing it to do the
deallocation later after the entire page is done processing and it goes
completely out of scope.
Granted, this isn't likely to give you major performance increases, but if
the object is large or holds valuable resources it could theoretically help
in some circumstances.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"John Saunders" <jo***********@ surfcontrol.com > wrote in message
news:eO******** ******@TK2MSFTN GP11.phx.gbl...
Steve,

Do you clean paper plates before discarding them? Then why set a variable
to null when it's going to disappear as soon as you leave the scope? :-)

--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uq******** ******@tk2msftn gp13.phx.gbl...
My mother always taught me to clean up after myself.
Old habits die hard.
:-)

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"John Saunders" <jo***********@ surfcontrol.com > wrote in message
news:OZ******** ******@TK2MSFTN GP10.phx.gbl...
Steve,

Why even bother setting the reference to Nothing? This isn't COM - it

won't
decrement the reference count.

--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uW******** ******@TK2MSFTN GP11.phx.gbl...
> Set it equal to nothing.
> You shouldn't need any fancy disposal stuff unless the class is holding open
> unusually valuable resources.
>
> --
> I hope this helps,
> Steve C. Orr, MCSD
> http://Steve.Orr.net
>
>
> "Carl" <ca**@nospam.co m> wrote in message
> news:92******** *************** *****@phx.gbl.. .
> > Hi,
> >
> > I have a class in .Net which I instantiate in my code
> > behind on one of my aspx pages.
> >
> > When I am finished with the instance, what is teh best
> > method of taking it out of memory. Should I set it to
> > nothing or can I dispose of it?
> >
> > Thanks,
> > Carl.
>
>



Nov 17 '05 #7
If those are the same Special Circumstances I was in about a year ago (and
for 15 months) then Good Luck.

I found a damp sponge to be very effective, if you then wash the sponge in
hot water...

--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"MS News (MS LVP)" <sq**********@h otmail.com> wrote in message
news:Oq******** ******@TK2MSFTN GP10.phx.gbl...
I Rinse Paper Plates Due to special circumstances

J
"John Saunders" <jo***********@ surfcontrol.com > wrote in message
news:eO******** ******@TK2MSFTN GP11.phx.gbl...
Steve,

Do you clean paper plates before discarding them? Then why set a variable to null when it's going to disappear as soon as you leave the scope? :-)

--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uq******** ******@tk2msftn gp13.phx.gbl...
My mother always taught me to clean up after myself.
Old habits die hard.
:-)

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"John Saunders" <jo***********@ surfcontrol.com > wrote in message
news:OZ******** ******@TK2MSFTN GP10.phx.gbl...
> Steve,
>
> Why even bother setting the reference to Nothing? This isn't COM - it won't
> decrement the reference count.
>
> --
> John Saunders
> Internet Engineer
> jo***********@s urfcontrol.com
>
>
> "Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
> news:uW******** ******@TK2MSFTN GP11.phx.gbl...
> > Set it equal to nothing.
> > You shouldn't need any fancy disposal stuff unless the class is

holding
> open
> > unusually valuable resources.
> >
> > --
> > I hope this helps,
> > Steve C. Orr, MCSD
> > http://Steve.Orr.net
> >
> >
> > "Carl" <ca**@nospam.co m> wrote in message
> > news:92******** *************** *****@phx.gbl.. .
> > > Hi,
> > >
> > > I have a class in .Net which I instantiate in my code
> > > behind on one of my aspx pages.
> > >
> > > When I am finished with the instance, what is teh best
> > > method of taking it out of memory. Should I set it to
> > > nothing or can I dispose of it?
> > >
> > > Thanks,
> > > Carl.
> >
> >
>
>



Nov 17 '05 #8
Steve,

I would want to see some research indicating that this is a good practice
"in general". Among other things, there may well be more overhead in
individually setting variables to null than in waiting until they go out of
scope all at once.

Also, our brains only have so much bandwidth. I'd rather spend mine on
writing code which actually _does_ something rather than on remembering to
figure out when I'm "done with" some variable just so I can give the garbage
collector a few more milliseconds to clean up a few thousand bytes on a 2Gb
server system - a few milliseconds it will probably not take advantage of.

Old folks like me need to be careful not to teach the kids our old bad
habits - even those which used to be good habits. The world has changed too
much to remain static. Machines have grown much faster and have larger
memories, and a lot of the habits I learned as a "kid" just don't matter
now. If I'd been learning today instead of 25 years ago, I would not have
been taught to set variables to null after use. I'd wonder what the big deal
was and why we couldn't just wait the few milliseconds until the end of
scope to get allow the Garbage Collector to collect this few megabytes in a
couple of seconds.
--
John Saunders (Old Dog)
Internet Engineer (the New Trick)
jo***********@s urfcontrol.com

"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uB******** ******@TK2MSFTN GP10.phx.gbl...
I agree that it is not a step that is necessary.
But in general it's good practice to free memory as soon as you're done with it.
By setting the object to nothing, you're permitting the garbage collector to pick it up immediately if it so chooses, rather than forcing it to do the
deallocation later after the entire page is done processing and it goes
completely out of scope.
Granted, this isn't likely to give you major performance increases, but if
the object is large or holds valuable resources it could theoretically help in some circumstances.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"John Saunders" <jo***********@ surfcontrol.com > wrote in message
news:eO******** ******@TK2MSFTN GP11.phx.gbl...
Steve,

Do you clean paper plates before discarding them? Then why set a variable to null when it's going to disappear as soon as you leave the scope? :-)

--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uq******** ******@tk2msftn gp13.phx.gbl...
My mother always taught me to clean up after myself.
Old habits die hard.
:-)

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"John Saunders" <jo***********@ surfcontrol.com > wrote in message
news:OZ******** ******@TK2MSFTN GP10.phx.gbl...
> Steve,
>
> Why even bother setting the reference to Nothing? This isn't COM - it won't
> decrement the reference count.
>
> --
> John Saunders
> Internet Engineer
> jo***********@s urfcontrol.com
>
>
> "Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
> news:uW******** ******@TK2MSFTN GP11.phx.gbl...
> > Set it equal to nothing.
> > You shouldn't need any fancy disposal stuff unless the class is

holding
> open
> > unusually valuable resources.
> >
> > --
> > I hope this helps,
> > Steve C. Orr, MCSD
> > http://Steve.Orr.net
> >
> >
> > "Carl" <ca**@nospam.co m> wrote in message
> > news:92******** *************** *****@phx.gbl.. .
> > > Hi,
> > >
> > > I have a class in .Net which I instantiate in my code
> > > behind on one of my aspx pages.
> > >
> > > When I am finished with the instance, what is teh best
> > > method of taking it out of memory. Should I set it to
> > > nothing or can I dispose of it?
> > >
> > > Thanks,
> > > Carl.
> >
> >
>
>



Nov 17 '05 #9
Non-Sense for some servers that are hit by 10s - 100s of thousands of users
per day
What about Database Connections?
What about FileHandles?
The good old rules still apply
Machines are faster and we have more RAM but We have more demand on these
machines
Things can go out of hand with the new generation
Think about it for few years

J
"John Saunders" <jo***********@ surfcontrol.com > wrote in message
news:eY******** ******@TK2MSFTN GP10.phx.gbl...
Steve,

I would want to see some research indicating that this is a good practice
"in general". Among other things, there may well be more overhead in
individually setting variables to null than in waiting until they go out of scope all at once.

Also, our brains only have so much bandwidth. I'd rather spend mine on
writing code which actually _does_ something rather than on remembering to
figure out when I'm "done with" some variable just so I can give the garbage collector a few more milliseconds to clean up a few thousand bytes on a 2Gb server system - a few milliseconds it will probably not take advantage of.

Old folks like me need to be careful not to teach the kids our old bad
habits - even those which used to be good habits. The world has changed too much to remain static. Machines have grown much faster and have larger
memories, and a lot of the habits I learned as a "kid" just don't matter
now. If I'd been learning today instead of 25 years ago, I would not have
been taught to set variables to null after use. I'd wonder what the big deal was and why we couldn't just wait the few milliseconds until the end of
scope to get allow the Garbage Collector to collect this few megabytes in a couple of seconds.
--
John Saunders (Old Dog)
Internet Engineer (the New Trick)
jo***********@s urfcontrol.com

"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uB******** ******@TK2MSFTN GP10.phx.gbl...
I agree that it is not a step that is necessary.
But in general it's good practice to free memory as soon as you're done with
it.
By setting the object to nothing, you're permitting the garbage collector to
pick it up immediately if it so chooses, rather than forcing it to do the deallocation later after the entire page is done processing and it goes
completely out of scope.
Granted, this isn't likely to give you major performance increases, but if the object is large or holds valuable resources it could theoretically

help
in some circumstances.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"John Saunders" <jo***********@ surfcontrol.com > wrote in message
news:eO******** ******@TK2MSFTN GP11.phx.gbl...
Steve,

Do you clean paper plates before discarding them? Then why set a

variable to null when it's going to disappear as soon as you leave the scope? :-)
--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
"Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
news:uq******** ******@tk2msftn gp13.phx.gbl...
> My mother always taught me to clean up after myself.
> Old habits die hard.
> :-)
>
> --
> I hope this helps,
> Steve C. Orr, MCSD
> http://Steve.Orr.net
>
>
> "John Saunders" <jo***********@ surfcontrol.com > wrote in message
> news:OZ******** ******@TK2MSFTN GP10.phx.gbl...
> > Steve,
> >
> > Why even bother setting the reference to Nothing? This isn't COM - it > won't
> > decrement the reference count.
> >
> > --
> > John Saunders
> > Internet Engineer
> > jo***********@s urfcontrol.com
> >
> >
> > "Steve C. Orr, MCSD" <St***@Orr.ne t> wrote in message
> > news:uW******** ******@TK2MSFTN GP11.phx.gbl...
> > > Set it equal to nothing.
> > > You shouldn't need any fancy disposal stuff unless the class is
holding
> > open
> > > unusually valuable resources.
> > >
> > > --
> > > I hope this helps,
> > > Steve C. Orr, MCSD
> > > http://Steve.Orr.net
> > >
> > >
> > > "Carl" <ca**@nospam.co m> wrote in message
> > > news:92******** *************** *****@phx.gbl.. .
> > > > Hi,
> > > >
> > > > I have a class in .Net which I instantiate in my code
> > > > behind on one of my aspx pages.
> > > >
> > > > When I am finished with the instance, what is teh best
> > > > method of taking it out of memory. Should I set it to
> > > > nothing or can I dispose of it?
> > > >
> > > > Thanks,
> > > > Carl.
> > >
> > >
> >
> >
>
>



Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1538
by: pesso | last post by:
I have an array of filenames, which are paths to the .NET assembly DLLs I built. I know for sure that these assemblies have public method, Execute(). In the run-time, I want to be able to instantiate these assemblies from my CS code and invoke the Execute function. Can I do this without setting the reference in the compilation-time or using...
2
2262
by: FredC | last post by:
S Name Microsoft Windows XP Professional Version 5.1.2600 Service Pack 2 Build 2600 Total Physical Memory 1,024.00 MB MDE 2003 Version 7.1.3008 ..NET Framework 1.1 Version 1.1.4322 SP1 Microsoft Visual C# .NET 69462-335-0000007-18707 Crystal Reports for Visual Studio .NET AAP50-GS0000S-WCK00C3 The code below shows the instantiation...
2
1314
by: HarishP | last post by:
Hi, How to avoid instantiating the class more than 10 times Harish.P Sr. Software Engineer Comat Technologies Pvt. Ltd., Bangalore Email: harish.p@comat.com
2
1235
by: active | last post by:
Because of an example I followed I've been instantiating Image objects. Now I notice that the documentation says the Image class is an abstract base class. Things seem to be working! Is the documentation wrong (it also says abstract classes cannot be instantiated)? Should I be using Bitmap instead? Is there a difference between...
6
1613
by: Gary Frank | last post by:
What are the ramifications if I were to instantiate an object tens of thousands of times and add them to an array? Or hundreds of thousands of times? Do you know if the act of instantiating a class takes a lot of storage or other resources? Would it be a severe performance penalty? From the .Net help doc:
1
2193
by: Marja Ribbers-de Vroed | last post by:
I've been provided with a custom ActiveX DLL (written in C++) that reads a certiifcate to generate a signature for a passed XML string, and I'm having trouble instantiating it. The DLL is registered succesfully on my computer and I've explicitly granted IUSR_<machinename> to use it. The DLL comes with a testtool (written in Visual Basic) which...
3
4936
by: Nagesh | last post by:
hi, I have seen the winvnc(tightvnc server) source code in this I seen that class member funtions are calling without instantiating the object i.e. like vncService::ShowDefaultProperties() where vncService is a class name not an refrence or instantiated object. is the above notation is possible or not?if yes how should i declare that...
1
3230
by: Bruce | last post by:
I am getting the following exception in the release build of my assembly. {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} System.Exception {System.AccessViolationException} I do not have any problems in debug. The problem occurs while instantiating an unmanaged class. Any idea...
3
1656
by: Randy | last post by:
Hi, I was learning about RTTI when I ran across this example. This line, out of the example, confused me. It is declaring a pointer to a base type and instantiating it with a derived class. I can say the words ... yet I don't get it. What do I have, a base or a derived? Can anyone push me in the right direction. abc *abc_pointer = new...
18
1851
by: RB | last post by:
Hi guys (and gals!), I've got 2 classes, "TypesafeConstant" and "Color". "Color" inherits from "TypesafeConstant", and adds no new functionality. All "Color" does is to instantiate some class variables which are public instances of "Color". What "TypesafeConstant" does is to create a shared list, and add every declaration of itself to...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8130
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.