creating new objects without assignment to a reference

  • Thread starter Thread starter Murat Ozgur
  • Start date Start date
M

Murat Ozgur

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 ?
 
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!
 
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 ?
 
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!
 
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,
 
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,
 
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.
 
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,
 
Ignacio Machin ( .NET/ C# MVP ) said:
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,

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.
 
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
 
Back
Top