Question re Garbage Collection

J

John Baro

I have object A on a form
In a method on the form I add object a to collection B which is local to
that method.
When that method is finished object B is out of scope but A is still in
scope because it belongs to the form.
Next time the GC runs will it see object B as collectable?

Methinks yes.

Or will it have to wait until B and all its refs and all their refs etc..
are out of scope?

No?

TIA

JB :)

What is the question?
That is the question.
 
M

Morten Wennevik

Hi John,

Yes, the collection falls out of scope so it is released, the individual
references inside the collection will be treated as individual and your
object A remains.

And no, it's a local object, it falls out of scope with the end of the
method.

Happy coding!
Morten
 
J

Jochen Kalmbach

Morten said:
And no, it's a local object, it falls out of scope with the end of the
method.

This is not 100% correct...
Sometime the (local) object is collected, while you are still inside the
scope of the method... (but the object will not be used in the future)

For more info about GC see:
Garbage Collection: Automatic Memory Management in the Microsoft .NET
Framework
http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx

Garbage Collection—Part 2: Automatic Memory Management in the Microsoft
..NET Framework
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/default.aspx


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp


Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
J

John Baro

Thanks Guys

I like it.

JB

Jochen Kalmbach said:
This is not 100% correct...
Sometime the (local) object is collected, while you are still inside the
scope of the method... (but the object will not be used in the future)

For more info about GC see:
Garbage Collection: Automatic Memory Management in the Microsoft .NET
Framework
http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx

Garbage Collection-Part 2: Automatic Memory Management in the Microsoft
.NET Framework
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/default.aspx


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp


Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
D

Dilip Krishnan

John,
Once out of the method, the collection B will no longer be a root
object and will be a candidate for collection. Hope that helps
Thanks
 

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