Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about island of isolation

 
Kaxhif Khan
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i am studying currently the topic of objects life cycle from K & B book. I have a minor confusion about the topic "island of isolation", let me post code here to explain my confusion..




K & B said that an object is eligible for garbage collection when it is no longer accessible. In the topic of "island of isolation" the child object of each object i.e. i2,i3 and i4 refer to each other. When we set i2, i3 and i4 equal to null i2, i3 and i4 becomes inaccessible (at line //do complicated) so far so good but what about child object of each object i.e. i2.i, i3.i, i4.i didn't they also become inaccessible at //do complicated..

i) how many objects are eligible for garbage collection at line //do complicated, K & B said '3' where i have a confusing that why '3' why not '6' ?
ii) What if we replace line 1, line 2 and line 3 with the following code, how many objects are eligible for garbage collection in new code ?


Thanks in advance ...
 
Sharmili Rameshbabu
Greenhorn
Posts: 27
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can easily know the number of objects created in the code. Number of objects=Number of new keywords!
So the answer to this question can be only less than or equal to three.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kaxhif,

First of all, a warm welcome to CodeRanch!

Kaxhif Khan wrote:K & B said that an object is eligible for garbage collection when it is no longer accessible. In the topic of "island of isolation" the child object of each object i.e. i2,i3 and i4 refer to each other. When we set i2, i3 and i4 equal to null i2, i3 and i4 becomes inaccessible (at line //do complicated) so far so good but what about child object of each object i.e. i2.i, i3.i, i4.i didn't they also become inaccessible at //do complicated.

K&B7 is correct! When an object can't be referenced anymore (from a live thread), the object is considered to be eligible for GC. The simplest example to illustrate:So because on line2 the reference variable o is set to null, the object created on line1 is not accessible anymore and thus eligible for GC. So rule of thumb: when an object is no longer referenced by at least one reference variable, the object is eligible for GC.

The "island of isolation" is a very specific/special case of this rule. If you look at the code snippet you posted and we focus on the Island object created on line number 5. At line3 there are 2 reference variables refering to this Island object: i2 and i4.i. When //do complicated is reached, there is still 1 reference variable refering to this Island object: i4.i. And the same applies for the 2 other Island objects created on line number 6 and 7. So it seems none of these objects are not eligible for GC, because for each object there is at least 1 reference variable which refers to this object and according to the rule of thumb, then the object isn't eligible for GC. But all the objects refer actually to each other (like they are on an island) and no other reference variable (from outside this island, the mainland) refers to one of the objects. Hence the term "island of isolation". So these 3 objects will be eligible for GC as well.

Kaxhif Khan wrote:i) how many objects are eligible for garbage collection at line //do complicated, K & B said '3' where i have a confusing that why '3' why not '6' ?

Because only 3 objects are created (on line numbers 5, 6 and 7).

Kaxhif Khan wrote:ii) What if we replace line 1, line 2 and line 3 with the following code, how many objects are eligible for garbage collection in new code ?

Then the answer would be 6 objects (but it wouldn't be an example of an "island of isolation").

Hope it helps!
Kind regards,
Roel
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As it was mentioned by Sharmili Rameshbabu, the objects are created in the following lines of code:



In these code passage you just assign references to previously created objects:



So you have only three objects. Local variables i2, i3, i4 are GC roots (am I right ?), so when you make them 'null', these objects are eligible for garbage collection, regardless that their 'i' properties point to each other.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmili Rameshbabu wrote:You can easily know the number of objects created in the code. Number of objects=Number of new keywords!

Not true!

You can create String objects without the use of the new keyword. And thanks to autoboxing you can create objects from primitive wrapper classes as well without the use of the new keyword.


Kind regards,
Roel
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergei Zhylinski wrote:

In these code passage you just assign references to previously created objects:





Sorry, in this lines of code:

 
Kaxhif Khan
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Roel,@Sharmili Rameshbabu and Sergei Zhylinski okey great discussion, my confusion is water washed ...!! Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic