473,397 Members | 2,099 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,397 software developers and data experts.

creating new objects without assignment to a reference

Hello,

I want to know about "creating new objects without assignment to a
reference" like this :

...
new Employee("John","Woo");
....

Is this a good programming practice ? How does garbage collector
handle this issue ?
Nov 17 '05 #1
9 5795
Hi Murat,

The object will fall out of scope when your code leaves the current code block and eaten by GC. You need some way of storing a reference as this is how GC determines if an object should be destroyed. No reference -> Eat it!

On Tue, 03 May 2005 09:29:25 +0200, Murat Ozgur <mu********@gmail.com> wrote:
Hello,

I want to know about "creating new objects without assignment to a
reference" like this :

...
new Employee("John","Woo");
....

Is this a good programming practice ? How does garbage collector
handle this issue ?


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
That object is immediate garbage unless it is being used as a parameter. If it is a parameter ...

static void Foo(object o)
{
}

static void Main()
{
Foo( new object() );
}

then in this case the object is rooted by the parameter o and when the method finishes (possibly before depending on the Foo implementation) the object will be garbage - unless it assigns the object to a field somewhere to extend its lifetime.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hello,

I want to know about "creating new objects without assignment to a
reference" like this :

...
new Employee("John","Woo");
....

Is this a good programming practice ? How does garbage collector
handle this issue ?

Nov 17 '05 #3
The object is never in scope - its not assigned to anything - its instant garbage as soon as the GC runs it will collect it - whether its in the code block or later

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi Murat,

The object will fall out of scope when your code leaves the current code block and eaten by GC. You need some way of storing a reference as this is how GC determines if an object should be destroyed. No reference -> Eat it!
Nov 17 '05 #4

The object IS in scope, the scope is the block where you declared it. all
the objects must have a scope

The object will be ready to be GC when the method is exited. You will be
able to access it though.

The line in question is equivalent to:

Type t = new Type();
t.SomeMethod();
t = null;
I usually use that construction when creating a win Form.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
The object is never in scope - its not assigned to anything - its instant
garbage as soon as the GC runs it will collect it - whether its in the
code block or later

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi Murat,

The object will fall out of scope when your code leaves the current code
block and eaten by GC. You need some way of storing a reference as this is
how GC determines if an object should be destroyed. No reference -> Eat
it!

Nov 17 '05 #5
Hi,

That object is immediate garbage unless it is being used as a parameter.
If it is a parameter ...

I don't believe this is the case, in any case it would be marked as GC
able, so the next time the GC runs it will be collected.

I haven't see the code generated for that line ( nor that I would know how
to interprete it either :) ) , but what I expect it to do is that it create
an anonymous variable, assign it and then set it to null, then it will be
collected as usual by the GC

This would be the easier to implement escenario I believe.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 17 '05 #6
Take the C# code

static void Main(string[] args)
{
new object();
}

this is the generated IL

.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 7 (0x7)
.maxstack 1
IL_0000: newobj instance void [mscorlib]System.Object::.ctor()
IL_0005: pop
IL_0006: ret
} // end of method App::Main

The point being that it simply calls the newobj opcode and doe not store a reference to the object anywhere. Therefore the object is immediately garbage (I don't mean that it gets collected I mean that it has no live root). This object will get collected as soon as the GC runs, even if it is at the next line of code. In addition it will not be marked as GC'able - nothing ever is. The fact that something is able to be GC'd is discovered by the GC at the point it runs, not before.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

That object is immediate garbage unless it is being used as a parameter.
If it is a parameter ...

I don't believe this is the case, in any case it would be marked as GC
able, so the next time the GC runs it will be collected.

I haven't see the code generated for that line ( nor that I would know how
to interprete it either :) ) , but what I expect it to do is that it create
an anonymous variable, assign it and then set it to null, then it will be
collected as usual by the GC

This would be the easier to implement escenario I believe.
Nov 17 '05 #7
Objects don't have scope, references to object have scope. Objects are either rooted not rooted.

The line in question is equivilent to exactly what the OP posted

new Type();

this does not store the reference anywhere and so is immediately garbage and unless it has a finalizer will be collected in the next gen0 collection.

Btw: in your code the setting of t to null doesn't achieve anything. Assuming a release build, as soon as t.SomeMethod() has executed the object can be collected - it doesn't have to wait until the reference goes out of scope (although it does have to in a debug build).

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

The object IS in scope, the scope is the block where you declared it. all
the objects must have a scope

The object will be ready to be GC when the method is exited. You will be
able to access it though.

The line in question is equivalent to:

Type t = new Type();
t.SomeMethod();
t = null;
I usually use that construction when creating a win Form.

Cheers,

Nov 17 '05 #8

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2******************@TK2MSFTNGP10.phx.gbl...

The object IS in scope, the scope is the block where you declared it. all
the objects must have a scope

The object will be ready to be GC when the method is exited. You will be
able to access it though.

The line in question is equivalent to:

Type t = new Type();
t.SomeMethod();
t = null;
I usually use that construction when creating a win Form.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


No, Richard is right. Your example is different as it assigns the reference
to t.

In this sequence...
...
new Employee("John","Woo");

...

.... new creates an object on the GC heap.
The object reference is stored on the execution stack, but as it's unnamed,
you don't ever refer (you can't) to the reference in your code, so it's
flagged as garbage and eligible for collection.

In an optimized build (/optimize), the reference is immediately popped off
the stack when newobj (C# new) returns and becomes garbage and as such
eligible for collection.

Willy.



Nov 17 '05 #9
Hi,

Thank both for the good explanation, I did some test and only then realise
the existence of the execution stack .
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:Oq****************@tk2msftngp13.phx.gbl...

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl...

The object IS in scope, the scope is the block where you declared it. all
the objects must have a scope

The object will be ready to be GC when the method is exited. You will be
able to access it though.

The line in question is equivalent to:

Type t = new Type();
t.SomeMethod();
t = null;
I usually use that construction when creating a win Form.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


No, Richard is right. Your example is different as it assigns the
reference to t.

In this sequence...
..
new Employee("John","Woo");

..

... new creates an object on the GC heap.
The object reference is stored on the execution stack, but as it's
unnamed, you don't ever refer (you can't) to the reference in your code,
so it's flagged as garbage and eligible for collection.

In an optimized build (/optimize), the reference is immediately popped off
the stack when newobj (C# new) returns and becomes garbage and as such
eligible for collection.

Willy.


Nov 17 '05 #10

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

Similar topics

5
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
7
by: Dave Townsend | last post by:
Hi, I came across a little problem the other day, I wonder if anyone has a more elegant solution: I have a class which has some "pixmap" members, this is a QT-data type which is similar to a...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
3
by: Dan Holmes | last post by:
I am creating an aspx application to use our existing COM objects. I referenced the component correctly and i have it Dimmed correctly. What i get is "InvalidCastException". Here is the code ...
8
by: acheron05 | last post by:
Hi there, Hopefully I won't fit the stereotype of the typical student posting his assignment to be written for him, but I am quite stuck. If anyone could give me some help in how to create...
27
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
1
by: Bartholomew Simpson | last post by:
I know this is a VB.Net ng. But hopefully, someone remembers some classic (VB6) coding - besides I've not had any response when I posted this to the VB6 specific ng. I wrote a C++ librray that I...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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...
0
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...

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.