Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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- Islands of Isolation

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
I think 3 objects are eligible for garbage collection.
 
author
Posts: 23868
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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: 23868
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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
 
please buy my thing and then I'll have more money:
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!