473,473 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

instansiate object

Hello!

When I instansiate some object is then a good point to set these object to
null just to help GC to
remove this.
I mean if I might improve performance.

//Tony
Sep 14 '06 #1
5 1619
Tony,

You shouldn't. In a non debug environment, you are actually prolonging
the object's lifetime by setting it to null.

Not doing anything is the best course here.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tony" <jo*****************@telia.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
Hello!

When I instansiate some object is then a good point to set these object to
null just to help GC to
remove this.
I mean if I might improve performance.

//Tony


Sep 14 '06 #2
Nicholas Paldino [.NET/C# MVP] wrote:
You shouldn't. In a non debug environment, you are actually prolonging
the object's lifetime by setting it to null.
In most cases it actually won't affect it either way - the object can
be collected before the assignment:

using System;

class Test
{
~Test()
{
Console.WriteLine ("Finalizer");
}

static void Main()
{
Test t = new Test();

GC.Collect();
GC.WaitForPendingFinalizers();

Console.WriteLine ("Setting t to null");
t = null;
}
}

Jon

Sep 14 '06 #3

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
| Nicholas Paldino [.NET/C# MVP] wrote:
| You shouldn't. In a non debug environment, you are actually
prolonging
| the object's lifetime by setting it to null.
|
| In most cases it actually won't affect it either way - the object can
| be collected before the assignment:
|
| using System;
|
| class Test
| {
| ~Test()
| {
| Console.WriteLine ("Finalizer");
| }
|
| static void Main()
| {
| Test t = new Test();
|
| GC.Collect();
| GC.WaitForPendingFinalizers();
|
| Console.WriteLine ("Setting t to null");
| t = null;
| }
| }
|
| Jon
|

Not sure what you mean here Jon, running the debug version will output:

Setting t to null
Finalizer

note that here the finalizer runs as a result of the AD shutdown,
while the release version shows:

Finalizer
Setting t to null

which actually illustrates Nicholas point, or am I missing your point.
Willy.
Sep 14 '06 #4
Willy Denoyette [MVP] <wi*************@telenet.bewrote:
Not sure what you mean here Jon, running the debug version will output:

Setting t to null
Finalizer

note that here the finalizer runs as a result of the AD shutdown,
while the release version shows:

Finalizer
Setting t to null

which actually illustrates Nicholas point, or am I missing your
point.
No, it doesn't illustrate Nick's point - it counters it. The only way
setting t to null could prolong the object's lifetime would be if the
GC kept the object alive due to the assignment. As the finalizer is
being run *before* the assignment, that shows the GC realises that the
variable isn't going to be *read* again, even though it will be written
- so the variable isn't treated as a GC root.

The assignment to null certainly doesn't help, but it doesn't actually
hinder the GC either.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 14 '06 #5

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
| Willy Denoyette [MVP] <wi*************@telenet.bewrote:
| Not sure what you mean here Jon, running the debug version will output:
| >
| Setting t to null
| Finalizer
| >
| note that here the finalizer runs as a result of the AD shutdown,
| while the release version shows:
| >
| Finalizer
| Setting t to null
| >
| which actually illustrates Nicholas point, or am I missing your
| point.
|
| No, it doesn't illustrate Nick's point - it counters it. The only way
| setting t to null could prolong the object's lifetime would be if the
| GC kept the object alive due to the assignment. As the finalizer is
| being run *before* the assignment, that shows the GC realises that the
| variable isn't going to be *read* again, even though it will be written
| - so the variable isn't treated as a GC root.
|
| The assignment to null certainly doesn't help, but it doesn't actually
| hinder the GC either.
|

Absolutely correct, I obviously mis-interpreted Nick's post.

Willy.


Sep 14 '06 #6

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

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: Nick Zdunic | last post by:
I have a remotable object running in my host application. The host starts up and creates the object. Within a method to start the remote object doing its thing it creates an object. ...
0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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...
0
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...
0
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 ...

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.