Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

garbage collection

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

prog1:

class X2{
public X2 x;
public static void main(String args[])
{
X2 x2=new X2();
X2 x3=new X2();
x2.x=x3;
x3.x=x2;

//x2=x3=null;
//x2=new X2();
//x3=x2;//line 9
}
}

in the above prog:

Objects x2 'n' x3 r eligible for gabage collection even though i didn't explicitly set them to null.

if i'm wrong pls correct.

Thank you
jaya
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what point are you wondering if they're eligible?

They'll never be eligible for garbage collection during the main method - you have active references to them. It doesn't matter that they refer to one another - the fact that you have references to them prevents them from being garbage collected.

When the main method ends, your references go out of scope so the objects would be available for garbage collection, but, at that point, the application terminates and no garbage collection is going to happen at that point. Rather, the JVM simply exits.
 
janne s
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Corey

Thank you. could u pls explain....

This from K&b book..

Islands of objects can be garbage collected even though they have references.



Thank you
jaya
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jaya,


How come u say that they are elligible for garabage collection.
No one is set to null, then how come will it be garbage collected.

arnab
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What the book says clearly, is that islands of isolation would be created only when the references x2, x3 are explicitly set to null.
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!