Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I picked this code from another fellow on JR forum


suppose named as Gc
we say Gc g=new Gc(new Gc(new Gc(null)))
or
Gc g=new Gc(new Gc(new Gc));
how many objects are created and how do you refer to those objects...

I am having problems with these questions.Can somebody help me with these..??
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you put code here??
Because from your explanation i didn't understand your problem!!!
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to explain you I have taken the full code from there:

at line 1, 3 objects are created
firste one refered by gb1, second refered by gb1.g, and third refered by gb1.g.g variables.
And at line 2, 2 objects are created first one refered by gb1.g.g and the second one refered by gb1.g.g.g variables.

Am i clear?
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you determine whether how may objects are eligible for gc???
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maggie joseph wrote:how do you determine whether how may objects are eligible for gc???


In general, or in this particular problem? I see in your profile you have posted quite a few topics on garbage collection. If you are still confused, perhaps you could explain where your confusion is?
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i am drawing a graph i get 2 objects eligible for Gc..how do you get answer as 3?in this program??
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There will be 3 object present on heap as explained by Neha. How did you get 2 objects on Heap???
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i am drawing a graph i get 2 objects eligible for Gc..how do you get answer as 3?in this program??

Here
Garb gb1 = new Garb(new Garb(new Garb(null)));
gb1 is referring to one garb object,then another instance gb1.g is referring to another garb object ,then third instance gb1.g.g is referring to third garb object which is a null object....M i right???
In second line
gb1.g.g=new Garb(new Garb(null));
we are referring gb1.g.g to a new object ...so the object it was referring first has no live reference referring it right???so it becomes eligible for Gc.....
now gb1.g.g refers to a new garb object and further gb1.g.g refers to another garb object which is a null object right???
In third line
Garb gb2=new Garb(new Garb(new Garb(gb1)));
gb2 is pointing to new garb object,gb2.g is pointing to another garb object and gb2.g.g is referring to garb object which is having reference variable gb1???now here by keeping gb1 in objects value's place what does it mean???
Fourth line
gb2.g=gb1.g;
According to fourth line i can conclude that the both gb1.g and gb2.g will refer to same object created by gb1.g.....then the object pointed by g2,g no longer has a live reference so it is eligible for gc....
i m confused with 3 line....and if gb2.g is leaving it pointer and referring to a new pointer what happens to gb2.g.g referenced object???i hope you have got my state of confusion.......

thanks
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this program, on line 1, 3 objects are created. Garb1,2,3.
Each Garb is having one reference variable, g. When Garb is created, g in that particular Garb is assigned a reference.

gb1---->Garb1.g-->Garb2.g-->Garb3.g-->null

Now, on line1 starting from rightmost side, first Garb3 (innermost) is created, its "g" is assigned null.
next, Garb2 is created and at the time of creation, Garb3 is passed to its one-arg constructor, "g" in Garb2 is assigned to Garb3.
similar for Garb 2 and 1.

then Garb1 object is assigned to gb1. so to access Garb3 using gb1 we use, gb1.g.g. Means using "g" in Garb2. likewise for others.

now on line 2:

gb1.g.g--->Garb4.g--->Garb5.g--->null;

this sets initial Garb2.g reference to Garb4
so it is like.
gb1-->Garb1.g--->Garb2.g--->Garb4.g--->Garb5.g--->null;

Garb3 eligible for GC.
now for gb2:

gb2--->Garb6.g--->Garb7.g--->Garb8.g--->gb1
means
gb2.g is "g" in Garb6 used to access Garb7 and we know gb1.g is "g" in Garb1 used to access Garb2.

gb2.g=gb1.g;

gives: gb2--->Garb6.g--->Garb2.g---->so on

Objects Garb7 and Garb 8 eligible for GC

hope this helps.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amazing explanation... ..thanks a lot......nw i am feeling good...
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're welcome
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey rushikesh....can you try explaining this to me???
question from this thread???
i didnt understand why m2=null???
http://www.coderanch.com/t/477390/Programmer-Certification-SCJP/certification/passing-variables-into-methods
 
Andre Enimot
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There m2 is not null, it points to the first Mixer object. but first Mixer's variable m1 is null, because first Mixer was created with no-arg constructor. so m2.m1 is null.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all people see this...this may help you get out of the garbage
http://radio.javaranch.com/corey/2004/03/25/1080237422000.html
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raju for this
I guess It has also helped you to move out of garbage
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice change of name.....but do look out for moderators warnings ....oops i hope this post doesn't increase the chance for that

and yes it certainly helped me get out
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if they warn i 'll change to ranccho
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if they lock your account....
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i guess they wont
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dont worry they wont....
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raju Champaklal wrote:dont worry they wont....


Actually, I think I'll ban the both of you... done. Bye bye.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic