Simple garbage collection question

R

RayLopez99

If you have an event handler like this:

private void button1_Click(object sender, EventArgs e)
{
myObject = new ObjectClass();
}

Everytime you click on button1 a new "myObject" is being created, and
(this is my question) the old one is being destroyed (garbage
collected), right? Since this is not an "object creating
factory" (not a class returning an object) I assume that is the case.

RL
 
G

Göran Andersson

RayLopez99 said:
If you have an event handler like this:

private void button1_Click(object sender, EventArgs e)
{
myObject = new ObjectClass();
}

Everytime you click on button1 a new "myObject" is being created, and
(this is my question) the old one is being destroyed (garbage
collected), right? Since this is not an "object creating
factory" (not a class returning an object) I assume that is the case.

RL

No, the old object is not being collected immediately when there is no
more references to it. It only becomes eligible for garbage collection.

The object may be collected when the next garbage collection happens.
 
A

Arne Vajhøj

RayLopez99 said:
If you have an event handler like this:

private void button1_Click(object sender, EventArgs e)
{
myObject = new ObjectClass();
}

Everytime you click on button1 a new "myObject" is being created, and
(this is my question) the old one is being destroyed (garbage
collected), right? Since this is not an "object creating
factory" (not a class returning an object) I assume that is the case.

Every time you click myObject will refer to a new object.

If there are no refs elsewhere for the object previously referred
to by myObject, then it will be ready for GC.

Whenever .NET think is a good time to do some GC then it will be
GC'ed at that time.

Arne
 
G

Göran Andersson

Michael said:
Since your code doesn't declare the object myObject, it is hard to
answer. If myObject is an application global, it won't be garbage
collected until the application exits. If it's a form global, it won't
be garbage collected until the form is disposed. Of course, if you then
in later code add myObject to another object or collection/list class,
it's lifetime then becomes dependent on the life of that object or
collection/list.

The OP is talking about the object that was referenced by the variable
before, but you are talking about the newly created object.
 
R

RayLopez99

The OP is talking about the object that was referenced by the variable
before, but you are talking about the newly created object.

Yes, that's the key I think. The object before (the old 'new'
ObjectClass();), is now just hanging out there, with nothing on the
LHS (left hand side) that equals it or is equated (identical with) to
it. So the old new object is a candidate for garbage collection,
assuming of course somewhere else in the code you don't reference it.

RL
 
G

Göran Andersson

RayLopez99 said:
Yes, that's the key I think. The object before (the old 'new'
ObjectClass();), is now just hanging out there, with nothing on the
LHS (left hand side) that equals it or is equated (identical with) to
it. So the old new object is a candidate for garbage collection,
assuming of course somewhere else in the code you don't reference it.

RL

Don't mix up the assignment operator (=) with the equality operator
(==). Assignment isn't any form of comparison.

The constructor method returns a reference to the created object, and
this reference is assigned to the variable. This overwrites the
reference that was there before.
 
P

Peter Bromberg[C# MVP]

Not necessarily "being destroyed" but when there are no references remaining
(as is the case in your button_click handler since the object goes out of
scope), it will be marked for garbage collection.
The Garbage man may not come right away, but you have it out on the street,
and he'll get it soon enough.
Peter
 
P

proxyuser

RayLopez99 said:
If you have an event handler like this:

private void button1_Click(object sender, EventArgs e)
{
myObject = new ObjectClass();
}

Everytime you click on button1 a new "myObject" is being created, and
(this is my question) the old one is being destroyed (garbage
collected), right?

The "old one" isn't necessarily destroyed when a new one is created. It's
destroyed at some point after there are no more references to it. If
myObject was the only thing referring to it, then it will be eligible for
destruction as soon as you reassign myObject, yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top