• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Garbage Collection- Islands of Isolation

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i have a doubt in the below example:
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;
// do complicated, memory intensive stuff
}
}
my understanding is:
1.there will be 3 reference variables created in stack..i2,i3&i4
2.for each these 3 refences, there are 3 objects in heap.
3.Inturn these 3 objects will be creating 3 objects each for their instance reference variable "i"..so there is 6 objects in heap.
4.when we do i2.i = i3;only the reference variable "i" of i2 changed to i3 and correspondingly in all other assignments.
5.so as per my undertanding we will loose refence of instance variable (i) of each object. and the the reference pointing from stack is still pointing to the i2,i3,i4 onjects in heap.
6.thus i m confused,how this 3 objects are available for garbage collection?
7.only the instance object(i) of each i2,i3,i4 created should be available for garbage collection know..

please make understand this example.

regards,
prabhu
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prabhu,

In order to understand this question, you need to know three simple concepts, may be you already knowing this, think about the following questions:

1) If I write

String str = null;

Is there any Object created on the Heap?

2)
class cl{
String str;
}

cl c = new cl();

What is the value of c.str ?? Is there any Object it is referring to ?


3) If i now write :
c = null;

If there any Object become eligible for garbage collection after the above statement executes?

When you answer all these question..try to apply your concept on your question..!! you will be having your answer and a smile on your face..!!
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunny,
i got it. just for my confirmation, for all your ans is "no"..am i right?

thanks & regards,
prabhu
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..

I didn't get the point...i couldnt answer the questions that sunny posted...someone please explain....
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 qusetion:
1) If I write

String str = null;

Is there any Object created on the Heap?

my answer: no
reason: only a refernce is created and no new objet is created on the heap

2 qusetion:
class cl{
String str;
}

cl c = new cl();

What is the value of c.str ?? Is there any Object it is referring to ?

answer: null,no
reason:str is an instance variable and it is automatically assigned null value and
hence c.str = null and it is not refering to any object

3) If i now write :
c = null;
If there any Object become eligible for garbage collection after the above statement executes?

answer: no
reason: no object is yet created so nothing is available to gc();

this my understaning.if anyone has other answer please correct me.
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think 3 objects are eligible for garbage collection.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

3.Inturn these 3 objects will be creating 3 objects each for their instance reference variable "i"..so there is 6 objects in heap.



Really?!? I see that the Island class has an internal reference to another island object -- but where is it "creating 3 objects each for their instance reference variable "i"" ?

4.when we do i2.i = i3;only the reference variable "i" of i2 changed to i3 and correspondingly in all other assignments.
5.so as per my undertanding we will loose refence of instance variable (i) of each object. and the the reference pointing from stack is still pointing to the i2,i3,i4 onjects in heap.



Yes, but what was the original values of i that you "loose"? Was those references refering to valid objects? Or were they null?

Henry
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now... assuming you are convinced that there isn't 6 objects, let's talk about the first three...

6.thus i m confused,how this 3 objects are available for garbage collection?
7.only the instance object(i) of each i2,i3,i4 created should be available for garbage collection know..



What about these instructions?



Don't these make the three original objects unreachable? Sure, they refer to each other. This means that if any one of them is reachable, they can reach each other. But none are reachable...

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic