Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Garbage collection, need some advice 1

Status
Not open for further replies.

Themuppeteer

Programmer
Apr 4, 2001
449
BE
Hello friends,


I know that the garbage collector of java removes objects without references.
Even if you have 2 objects A and B, and if A has a reference
to B, and you remove all links to A, it will remove A and B (since A is an object with no references to it).
But what if A has a reference to B, and B has a reference to A , and no other references to those objects are made anymore, will the garbagecollector detect that ?
And what if you push it a little further, and take 4 or 5 or even 'N' objects all connected to each other in a circle but with nothing else connected to them (so they live on an island),will the garbage collector detect that ?

A : removes A
A-->B : removes A and B
A<-->B : ?

A-->B-->C : ?
^_______|


I think (hope) it will, but I'm not sure and I have to be sure about this one.
Advice/opinions are welcome.

Best regards,
themuppeteer


Greetz,
img


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

&quot;Those who say they understand chess, understand nothing&quot;

-- Robert HUBNER
 
>>>> But what if A has a reference to B, and B has a reference to A

I A and B only reference each other, I believe that once either is released (ie null), then both will be GC'd.


I could be wrong though !

I found this link helpul in the past - it's about memory leaks, but explains GC quite well ...
 
The link you gave was EXACTELY what I needed!
thank a lot!

Greetz,
img


NOSPAM_themuppeteer@hotmail.com (for mails, remove the NOSPAM_)

&quot;Those who say they understand chess, understand nothing&quot;

-- Robert HUBNER
 
Themuppeteer,

When two objects A and B point to each other, and no one else
can access these objects, the situation is called &quot;An Island.&quot;
-exactly what you said. GC will definitely collect this garbage. This item is covered in the materials for Java Programmer's Exam.


~za~
You can't bring back a dead thread!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top