How the GC reclaim the memory of the objects with circled reference

B

Bill

Let's simplify using public data member. Say I have (the code should
look same in C# and Java):

Class A
{
public B m_b;
}

Class B
{
public A m_a;
}

A a = new A();
B b = new B();
a.m_b = b;
b.m_a = a;

When a and b are not referenced by any other objects, a and b still
reference to each other. How does .NET know that a and b should be
garbage collected?

I raised the same question when JDK 1.0.x just came out, but don't
recall that I saw a concreate answer (maybe I missed it somehow). I
am pretty sure that Sun is handling it correctly in their current JVM.
If anybody knows how it works in JVM, please make your comments here
too. Thank you!

Regards,
Bill
 
R

Richard A. Lowe

Very simple - the GC marks EVERYTHING as collectable, then walks though all
the references in every stack frame marking all referenceable objects (and
their child objects) as non-collectable. Objects that reference each other,
but have no 'live' reference in the current or parent stack frames will
remain marked collectable.

Richard
 

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

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top