This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects eligible for GC

 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Deavaka's ExamLabSimulator scjp 5


According to me only 1 object must be available for GC.. but the answer is 2..(Have figured out using pen and paper)
How is that???

I want to know where i went wrong and wanna rectify before appearing the scjp exam..
 
rewati raman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c.aob=a.aob();
this looks not correct.
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rectified the code
 
vendikonda sravan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shahid i'm jus try in to answer to this but not sure this is correct , i request experienced ranchers please do correct this if wrong.

Total no. of objects got created are 4
1.A a=new A(); //Object one
2. A b=new A(); //Object two
3. A c=new A(); //Object three
4.A d= new A().aob=new A();////Object four

As c.aob = null; we can not be access(reach) object created on line 4 above so available for GC.

then in line "c=b"now c also points(refers)the same object which got created on line 2 above
so the object which got created on line 3 is not reachable so available for GC .

Thanks
Sravan
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A d=new A().aob=new A(); // fourth object also made to point to fifth object


Hi Sravan.. the line above creates 2 objects .. and it has no links with the other objects created there.. and according to me the object that will be GCted id c.aob as it will be referred to null;;
A a=new A(); // first object
A b=new A(); // second object
A c=new A();// third object

creates 3 objects then
a.aob=b;
will point to what b is pointing (i.e second object)

b.aob=a;
will point to what a is pointing (i.e First object)
c.aob=a.aob;
c.aob will point to what a.aob is pointing (i.e second object)
c=b;
c will be made to point to what b is refering(i.e second object)
but the object which c was refering earlier is till refering to second object(c.aob=a.aob)
c.aob=null;
and this is where that object( c.aob refering to what a.aob was refering) is made null ;

so just 1 object;;;

guys please correct me if i am wrong!



 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Basically, you need to remember that objects are GC'ed, and not references. So... you need to track the objects and their reachability -- and not whether something is set to null or not.

Anyway, two objects will be gc'ed. They are...

1. The object created at line 6, will become unreachable after line 11.
2. One of the objects created at line 10, will become unreachable after the same line (line 10).

Henry
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the objects created at line 10, will become unreachable after the same line (line 10).

how???

the other one is correct.. but my question is how is this happening at line 10 that one of the object here becomes unreachable ??
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohammad shaid wrote:
but my question is how is this happening at line 10 that one of the object here becomes unreachable ??


Let's turn the question around... can you tell us, how the two objects created at line 10 are both still reachable?

Henry
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A d=new A().aob=new A();

d reference var is pointing to one object which is made to point to another object.the link is not broken... In short no object is released..

thats the way i am looking at it.. please clarify how we need to look at this ??
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohammad shaid wrote:
thats the way i am looking at it.. please clarify how we need to look at this ??



Well... it looks like this ...



This expression...



evaluates to something like this...



The d reference is assigned to the result of the other assignment. And the result of the assignment is the RHS of the assignment.

Henry
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

(aob)
Obj1 ------------> Obj2
^
|
|
|
d ------------------------+


so obj1 here becomes GCed... thanks a lot brother.. i got it now...
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is Like

A d = Instance-Varibale = new A();

Therefore d= same #s , the one goes into Instance variable

obj1--->obj2


d-------->Obj2 and (d is not pointing to Obj1)

Hence obj1 is also eligible for GC.
Other Object eligible for GC is indeed clear.

Hence asnswer is 2 and not 1.

Tricky Question Indeed.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic