• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

garbage collection

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friends
i was going through the k&b book for scjp1.4 and i do not understand how the following code works.please help me

public class Island{

Island i;
public static void main(String [] args){

Island i2=new Island();
Island i3=new Island();
Island i4=new Island();

i2.i=i3;//i2 refers to i3
i3.i=i4;//i3 refers to i4
i4.i=i2;//i4 refers to i2




i2=null;
i3=null;
i4=null;


//more stuff


}
}


now in the book it says that the above code makes the three objects eligible for garbage collection.but doesn't it set the instance variable i's value three times.how is the references to the objects are getting deleted?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When an object (e.g i2) is set to null, the object becomes eligible for garbage-collection.

Also, the member variables of the object become eligible for garbage
collection. (assuming there is no other reference to the member variable)
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kuturam, if a object(outer) has another object(inner),then it is not necessary for object(outer) to become eligible for garbage collection to make object(inner) to be eligible for garbage collection, so in your case, all the three objects are assigned to null, all those will be eligible for gc without waiting for object to become eligible for gc that each object has..

i hope you got the point.
 
kuturam chakraborty
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the book they called this islands of isolated reference variables.such that every object has an alive reference but still they are still eleigible for garbage collection.

I don't understand how each object is having a valid reference?
 
author
Posts: 9035
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Kuturam,

Let me ask you a question. After all the three statements have run that set references to null, can you find a reference to the object previously referred to by i2? How about the so-called i3 and i4 objects?

Or, in other words, how many reference variables are in this piece of code?

hth,

Bert
 
kuturam chakraborty
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Bert,
Thanks for taking time out to answer my querry,byt i still have a doubt .Let ME TELL YOU HOW I UNDERSTAND THE QUESTION

In the first three lines inside the main method three objects are created and the three reference variables i1,i2,i3 refer to them respectively

now after the i2.i=i3; line executes the object peviously referred to only by i3 now has 2 references i and i3.

after the i3.i=i4 executes the object referenced by i4 has 2 references i,i4 but the i3 object is referred only by i3 now.

similarly after the i4.i=i2 executes now i2 object is referred to by i and i2 but the other two objects have just single references.

so at this point there are three objects one being refered to by the i & i2,another refered by i3 and another by i4.

now after the following code runs

i2=null;
i3=null;
i4=null;

there still remains the i reference variable which referes to the object originally refered by i2.

so 2 objects are eligible for gc not three objects.

Please let me know where i am going wrong.

Let me take this oppurtunity to thank you and Kathy Sierra for writing a wonderful book.You have really made learning java enjoyable.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Kuthuram, u r missunderstanding here...

1) u r thinking 'i' is same for all 'i2','i3' & 'i4'(which would have correct if 'i' was static var). But here 'i' is instance var. So all 'i2.i','i3.i' & 'i4.i' are different.

2) Actually...

'i2' is having two references --> 'i2' & 'i4.i'
'i3' is having two references --> 'i3' & 'i2.i'
'i4' is having two references --> 'i4' & 'i3.i'.


At some point all the 'i2','i3'&'i4' are set to null.
Now u tell me "How can u access 'i' var of any of the 3 obj?"

U can't access them , right?

If u can't access 'i' how can u access object represented by 'i'?.
U can't .

As any live thread can't access the objects 'i2','i3' & 'i4' they become eligible for GC.

I hope u have understood...

 
kuturam chakraborty
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mahendar

Is this why it is said that instance variables are instance specific while the static variables have just one copy?

I am preparing to sit for the scjp in about 2 weeks time .please advise me on how i can utilize these two weeks to the maximum.I am following the K & B book and I have alredy done it twice and plan to sit for the mock exams provided with the book very soon.How much should i get in the mock exam to consider myself to be prepared.
 
Bert Bates
author
Posts: 9035
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh no kuturam!

You're worrying me now - how did static variables enter into this question?
 
kuturam chakraborty
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Bert i really got confused.Its ok now, I went through the entire gc portion and also the figure given along with it in the book.i had actually missed the figure the first time .
 
reply
    Bookmark Topic Watch Topic
  • New Topic