aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
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

Joined: Oct 13, 2009
Posts: 156
Can you put code here??
Because from your explanation i didn't understand your problem!!!


Sage of The Monstrous Toad of Mount Myoboku
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
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?


SCJP 1.6 96%
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
how do you determine whether how may objects are eligible for gc???
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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?


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
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

Joined: Oct 13, 2009
Posts: 156
There will be 3 object present on heap as explained by Neha. How did you get 2 objects on Heap???
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
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

Joined: Dec 22, 2009
Posts: 65
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.


SCJP 5.0 100%
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
amazing explanation... ..thanks a lot......nw i am feeling good...
rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
you're welcome
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
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

Joined: Jul 29, 2009
Posts: 31
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

Joined: Dec 10, 2009
Posts: 521
all people see this...this may help you get out of the garbage
http://radio.javaranch.com/corey/2004/03/25/1080237422000.html


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
Thanks Raju for this
I guess It has also helped you to move out of garbage
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
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

Joined: Nov 04, 2009
Posts: 114
if they warn i 'll change to ranccho
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
what if they lock your account....
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
Well i guess they wont
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
dont worry they wont....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

"Phungsuk Wangdu" please check your private messages for some administrative matter...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Raju Champaklal wrote:dont worry they wont....


Actually, I think I'll ban the both of you... done. Bye bye.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection