newbie garbage collection confusion

G

Guest

Hello all. I'm learning C# after years of C++ and I'm having some trouble getting used to the concept of "garbage collection". I understand that an object is created using the "new" keyword and it doesn't require manual de-allocation due to the garbage collection scheme. But what about scope? In C++ a dynamically allocated object is persistant until it is de-allocated. What happens in C#? Is it flagged for cleanup when it exits the local code block? If so, how do I make an object persistent? If not, when I'm done with the object how should I indicate to the framework that it should flag that object for garbage collection?

Thanks in advance, everyone. I can see GC will take some getting used to...it runs counter to all my instincts!
 
J

Jon Skeet [C# MVP]

Lionel said:
Hello all. I'm learning C# after years of C++ and I'm having some
trouble getting used to the concept of "garbage collection". I
understand that an object is created using the "new" keyword and it
doesn't require manual de-allocation due to the garbage collection
scheme. But what about scope? In C++ a dynamically allocated object
is persistant until it is de-allocated. What happens in C#? Is it
flagged for cleanup when it exits the local code block?

No. An object is eligible for garbage collection when there are no live
references to it - basically when nothing can get to it. It will only
be actually collected when the garbage collector next runs. (There are
some extra concepts too, like finalization and generations - but let's
ignore those for the moment.)
If so, how do I make an object persistent? If not, when I'm done with
the object how should I indicate to the framework that it should flag
that object for garbage collection?

Things generally "just work". Objects will naturally end up becoming
eligible for garbage collection at the right time, usually.
 
B

Ben Lucas

From .NET documentation:

"Unlike COM, the common language runtime does not use reference counting to
govern object lifetime. Instead, the garbage collector traces object
references and identifies objects that can no longer be accessed by running
code.
This simplifies component programming a great deal, because you do not have
to worry about circular references. If a group of objects contain references
to each other, but none of these object are referenced directly or
indirectly from stack or shared variables, then garbage collection will
automatically reclaim the memory."

Basically, the garbage collector determines what is garbage by checking to
see if the object is referenced "directly or indirectly from stack or shared
variables" meaning that the garbage collector will know if an object is able
to be used. If not, it may be garbage-collected.

Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

Lionel said:
Hello all. I'm learning C# after years of C++ and I'm having some trouble
getting used to the concept of "garbage collection". I understand that an
object is created using the "new" keyword and it doesn't require manual
de-allocation due to the garbage collection scheme. But what about scope?
In C++ a dynamically allocated object is persistant until it is
de-allocated. What happens in C#? Is it flagged for cleanup when it exits
the local code block? If so, how do I make an object persistent? If not,
when I'm done with the object how should I indicate to the framework that it
should flag that object for garbage collection?
Thanks in advance, everyone. I can see GC will take some getting used
to...it runs counter to all my instincts!
 

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