aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collect question 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 collect question" Watch "Garbage collect question" New topic
Author

Garbage collect question

Gil Steidle
Ranch Hand

Joined: Nov 19, 2010
Posts: 30
Question from Exam Lab's Java Programmer 6.0

How many objects are eligible for garbage collection after c.aob=null?
The correct answer is 2. I can only find but 1.
The A() object that was created by c=new A() is the one that I found as eligible for gc.

Thanks,
Gil


Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
You should search the forum before posting a question, It has been asked before.

For now, I can give you an explaination:

c.aob = a.aob and a.aob =b so effectively, c.aob = b.
So when c.aob = null means b = null now b also has and object b.aob which will also be eligible for gb if b is nulled.
So, there are two objects one referred by 'b' and one that is referred by 'b.aob'.

HTH


SCJP 1.6 96%
Jurica Krizanic
Ranch Hand

Joined: Dec 11, 2008
Posts: 38

Gil Steidle wrote:Question from Exam Lab's Java Programmer 6.0

How many objects are eligible for garbage collection after c.aob=null?
The correct answer is 2. I can only find but 1.
The A() object that was created by c=new A() is the one that I found as eligible for gc.

Thanks,
Gil





I also thought that 1 object is eligible for GC because the line



made a confusion. But I modified the code a little bit (see below) to understand better.
As you can see, I have added a code instance variable and constructor which receives an integer, and sets the code variable to the value of the received integer.
And also I have added one println statement to see what is a code of object which d points to.
If you try to run it, it should print 5.
Before this code modification I thought d will point to the object with code=4.








Jurica Krizanic - Java developer, OCPJP6, Spring Core certified developer!
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Neha Daga wrote:You should search the forum before posting a question, It has been asked before.

For now, I can give you an explaination:

c.aob = a.aob and a.aob =b so effectively, c.aob = b.
So when c.aob = null means b = null now b also has and object b.aob which will also be eligible for gb if b is nulled.
So, there are two objects one referred by 'b' and one that is referred by 'b.aob'.

HTH


Hello Neha...I am slightly in doubt of your explanation, because look CAREFULLY... a.aob, b, c, and c.aob(This last one through a.aob) ALL refer to b...This means That The FIRST object to be ELIGIBLE for garbage collection is the object referred to by c.
Now when c.aob is set to null it means ALL references that refer to b are set to null!!. Hence making object referred to by b ELIGIBLE for garbage collection
Summary: Objects referred to by b AND c are ELIGIBLE for garbage collection.

IF YOU SAY OBJECT REFERRED TO BY a IS ALSO ELIGIBLE...CAN YOU PLEASE TELL US WHY OBJECT REFERRED TO BY c IS NOT??
BECAUSE "c=b" SIMPLY MEANS c NOW REFERS TO THE SAME OBJECT AS b. AT LEAST THATs WHAT K&B's BOOK TAUGHT ME.

WHEN b IS GARBAGE COLLECTED, ITS "aob" IS SIMPLY GARBAGE COLLECTED AS WELL, BUT a's OBJECT IS NOT a IS NOT SET TO null.
FINALLY MY THEORY MIGHT BE WRONG BECAUSE I AM A SCJP/OCP STUDENT LIKE YOU ALL, CAN SOMEONE HELP US CLARIFY THIS?¿? Thanks.

OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

Hello all...
Actually 2 objects are garbage collected.
carefully look at this code..

Here object created in the first assignment expression is lost ,it's instance variable is in turn assigned a new object which is stored in d.

Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hareendra Reddy wrote:Hello all...
Actually 2 objects are garbage collected.
carefully look at this code..

Here object created in the first assignment expression is lost ,it's instance variable is in turn assigned a new object which is stored in d.


Hareendra Hi! Here You are again! Thanks for your contribution.
I modified your code from a 'One-Liner'...And The reference was NO LONGER null!! Take A look.


Haven said that I still have SERIOUS doubts because the JVM
will see that d.aob CAN refer to the new Object and hence
'SHOULD NOT' be eligible for GC.

Think carefully about objects garbage collection ELIGIBILTY...
It is when NO reference variable CAN refer to an object that is when AND ONLY when it is ELIGIBLE.

This is REALY cracking my brains!...NOTHING in Java is a piece of cake!
Hareendra Reddy
Ranch Hand

Joined: Jan 09, 2011
Posts: 173

ease up jackzia, actually second part of your code is different ...

here d refers to the new object created but in the original statements this object is lost but it's instance variable of same type is
assigned reference to the yet another object and also to d.

We can imagine original code like this...


Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hareendra Reddy wrote:ease up jackzia, actually second part of your code is different ...

here d refers to the new object created but in the original statements this object is lost but it's instance variable of same type is
assigned reference to the yet another object and also to d.

We can imagine original code like this...


Hareendra thanks!... I am usually eased up...Its just that sometimes when I think I got it right,
something new 'pops-up' and says "wait a minute! we arent done yet"...This is Java...
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Ikpefua Jacob-Obinyan wrote:
Hareendra Reddy wrote:ease up jackzia, actually second part of your code is different ...

here d refers to the new object created but in the original statements this object is lost but it's instance variable of same type is
assigned reference to the yet another object and also to d.

We can imagine original code like this...


Hareendra thanks!... I am usually eased up...Its just that sometimes when I think I got it right,

something new 'pops-up' and says "wait a minute! we arent done yet"...This is Java...

Harrendra Take A look at the following...

SOURCE: ExamLab


Can you guess how many objects are ELIGIBLE for GC?? My answer is none! lol
I always use pencil and paper to sketch the heap and references to the heap
And I noticed that gab2.g can reference gab1 AND gab3 respectively.

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
FWIW,

a line of code like this:



won't appear on the exam.

I am curious to know, would you ranchers be okay to see a line of code like that in the real world?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Francisco J. Bermejo
Greenhorn

Joined: Mar 02, 2011
Posts: 27

Ikpefua Jacob-Obinyan wrote:
Harrendra Take A look at the following...

SOURCE: ExamLab


Can you guess how many objects are ELIGIBLE for GC?? My answer is none! lol
I always use pencil and paper to sketch the heap and references to the heap
And I noticed that gab2.g can reference gab1 AND gab3 respectively.



Hi Ikpefua, as I see it, two Gabc objects are eligible for GC: gab1 and gab3. This is because its references are assigned to null at the end of the code, and gab2.g is still assigned to null. No one changes that assignment, so I don't understand why you say "I noticed that gab2.g can reference gab1 AND gab3 respectively". If I'm wrong in any reasoning please tell me so .

BTW, Bert thank you for clarify to us that this horrible piece of code is never used in the real exam. It makes me shiver!


OCPJP 6 96%
Prasanna l reddy
Greenhorn

Joined: Mar 14, 2011
Posts: 12
hi,

As I see there are no objects aligible for GC eventhough gab1 and gab3 are null. These two(gab1 and gab3) are still refer to gab2...so I think non eof them are eligible for GC...

Can anyone tell how 2 objects are eligible for GC??? Thanks in advance.

Thanks,
Lakhmi
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi to all,

In the code the line no 11,
c=b;
c points to the object b.Then how c.aob=null makes b = null?
i think its make only b.aob=null

I can't understand.Please clarify my doubt.

Thanks


Cheers Munees
My Blog
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Muneeswaran Balasubramanian wrote:
In the code the line no 11,
c=b;
c points to the object b.Then how c.aob=null makes b = null?
i think its make only b.aob=null

I can't understand.Please clarify my doubt.
Thanks

Hello Muneeswaran you are correct, to the best of my knowledge, if you set a reference variable to null, it ONLY affects the reference variable and NOTHING more.
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Jacob,

I have change the code slightly,



By the finalize method and static variable i get the no of objects are eligible for garbage collection is 2.
Bit its not c and b,I think its c and another one is d.aob?

I am little bit doubt about d.aob is garbage collected?
Am i right?
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Muneeswaran,
When it comes to Garbage collection 'An image is worth a thousand words', if you really want to put an end to the 'headache' that garbage collection can give sometimes, I advise you to get a pen and paper and practice regularly drawing diagrams of the heap. Please do find attached a diagram of your program, study it very CAREFULLY and tell me what you think.


[Thumbnail for The_Heap_aob.png]

Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Take note that the numbers placed in the null references corresponds to the lines of codes in your program AFTER whose execution the references become null.
REMEMBER that objects are 'eligible' for garbage collection when there are NO live references connecting them.

I hope this helps.

Regards

Ikpefua.
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi jacob,

Thanks for your valuable tips.I have already try with the diagram.

In your explanation diagram, you have mentioned the line 22 makes the null reference,How it makes that?

A d = new A().aob=new A()

I think the above line makes the reference d refers the object A thru only the object A.aob.Then how you make it as null reference in line 22.

I can't get you.Please explain.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Muneeswaran wrote:
I "think" the above line makes the reference d refers.....


Hello Muneeswaran,
in java it is good to "think", after "thinking", you MUST put into "PRACTICE" what you "think" to know if your "thought" is correct.

Now lets do some "PRACTICE"...

In java expressions are evaluated from left to right, the following program is going to evaluate line 22 'step by step' from 'left to right', please observe CAREFULLY.


-Take NOTE that your JVM may give you different ID's, my JVM gave me the above ID's.
-The ID is the 'identification' assigned by the JVM to every Object instantiated
-Did you NOTICE that the ID's are NOT the same?
-Did you NOTICE that the first object becomes 'eligible' for G.C after the second assignment operation takes place?

I hope this helps.

Regards

Ikpefua

PS: The author -Bert Bates- has made it clear that this line of code:

Is NOT on the exams, I decided to explain to you because on-the-job you may find some funny codes like this and now you know what to do.



Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Jacob,

Thanks for your kindness and your explanation.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855



In this particular statement, reference d is assigned whatever referred be the new A().aob, in this case the object created by the right most part of that statement (new A()). Here the intermediary object (which is created by the middle part) is abandoned, that is there's no live reference from the program pointing to it.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 114

Finally correct Answere is : 2 Objects are eligible for GC. right???
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Vijitha Kumara wrote:

In this particular statement, reference d is assigned whatever referred be the new A().aob, in this case the object created by the right most part of that statement (new A()). Here the intermediary object (which is created by the middle part) is abandoned, that is there's no live reference from the program pointing to it.


@Vijitha this is a very simple and CLEAR explanation... However I noticed with past experience that people understand BETTER when you provide DETAILED sample programs and draw a diagram like I did above. You know it is as a result of the complex nature of the java language.
I guess this thread has perfectly explained this topic enough, thanks for your participation.

Regards

Ikpefua
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
Ikpefua Jacob has done a good explanation on this topic here is just an addition to what left off.

This line of code confused us if we don't draw a diagram or break it down the statement. To understand
better I've breaked the code down into pieces for easy to follow. Draw a a diagram yourself and see if it makes sense...

Analysis: A d = new A().aob = new A();
Starting from left to right from the assignment equal "=". Remember the RULE is always evaluate statement on the RIGHT
of assignment "=" FIRST, once complete then assign to the LEFT of the assignment "=".

1. new A().aob in and points ----> (A) newly created object on the heap hence keyword new A().
Recap from K&B book: "This also called "islands of isolation" is another way in which objects can become eligible for garbage collection,
even if they still have valid references."

2. d = A().aob //Assign A().aob to d, and A().aob is NO longer pointing to (A)
3. d now points to ----> A().aob NOT pointing to an object (A) so, nothing pointing at object (A).
4. Object (A) now available for GC.


In summary, there are 2 objects available for GC ONE pointed by "c" and ONE in
object A() due to assignment on c=b and c.aob=null.

On the actual EXAM you won't see anything like this, it's much easier, but tricky.


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Ikpefua Jacob-Obinyan wrote:@Vijitha this is a very simple and CLEAR explanation...

You're welcome
...that people understand BETTER when you provide DETAILED sample programs and draw a diagram like I did above

Agree. Specially in GC related stuff, diagrams make them easier to understand (and you've done a good job there).
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

@Tommy...Excelent explanation even if everything has "PRACTICALLY" been made clear it is always good to observe analysis from different perspectives.

My 'intuition' tells me that you are/were a C++ fan, I noticed your constant use of 'points to'...
Thanks for the compliments.

@Vijitha your simple analysis couldnt be better, thanks for the compliments.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collect question