Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Objects eligible for GC

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: Get Certified Exam Lab, by Devaka Cooray


Question: How many objects are eligible for GC after the c.aob = null call?

Answer given: 2 objects

But, I thought the answer is 1 object, which is the object that was originally referenced by the variable c...

How does it turn out to be 2?
[ November 15, 2008: Message edited by: Rekha Srinath ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rekha,
My suggestion would be, you have to plot the mapping in a graph and add/remove references as you go reading the code, when solving GC related problem. It's quickest way and you wont missout anything. I've written an article Object's eligibility factors for Garbage Collection in Java]Object's eligibility factors for Garbage Collection in Java for the same, hope it might hep you.
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Manivannan. I did that. Based on that only, I decided that only one object is eligible.

My doubt is in this:
There is an A object referenced by d. And this object's aob reference refers another A object. (which is done by the statement A d=new A().aob=new A();

I assume that this is valid, and that both these objects are not eligible for GC, which finally leaves me with just one object eligible for GC
[ November 15, 2008: Message edited by: Rekha Srinath ]
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rekha, this question is already discussed in the following topic:
https://coderanch.com/t/417827/java-programmer-SCJP/certification/GC
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh thanks Kenneth for the link.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm.... This would be an interesting one to draw out.

Let's name the objects OA, OB, OC, etc., as they are being created. This is to distinguish them from the references. Also, let's follow the aob reference of the objects, in order to get the full graph. So...

---------------------------
After:

A a=new A();
A b=new A();
A c=new A();

We look like this...



---------------------------
After:

a.aob=b;
b.aob=a;
c.aob=a.aob;

We look like this...



---------------------------
After:

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

We look like this...



note: object OD is not reachable after this line.

---------------------------
After:

c=b;
c.aob=null;

We look like this...



So... We lost access to objects OC and OD. Those two objects are no longer reachable.

Hope this helps, and please correct me if I missed anything...
Henry
[ November 15, 2008: Message edited by: Henry Wong ]
 
Kenneth Lomvey
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for your great explanation.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got 1 object as well, Rekha. I think the trick is in
the line A d=new A().aob=new A(); which Henry Wong's graph
shows actually results in an anonymous A being created, which,
unlike d, gets it's object field initialized with another A,
and THIS is what d points to. So the trick is that after
that line, the anonymous A object is already eligable for
gc, without anything having to be nulled.

Is that right?
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes thats the catch Ken..
 
Sheriff
Posts: 6640
1263
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rekha,

I think that the problem is now solved. Thanks for Henry, he gave a nice explanation.

I'm sorry, that I didn't include any explanation for GC questions in my simulator. I think I should include them in the next version.

Thanks for discussing this question.


Cheers
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic