• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection

 
Edward Lawrence
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why isn't c3 eligible for garbage collection in the following question? Doesn't it equate to null after c1.go(c2) returns?

QUESTION: - When // doStuff is reached, how many objects are eligible for GC?

 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where the question is from.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thread with the source
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Rob. I hadn't noticed the other thread.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c3 is a reference - it is never eligible for garbage collection.

on line 8, an object is created, and c1 is made to refer to it.
on line 9, an object is created, and c2 is made to refer to it.

on line 10, we call the go() method of c1, and pass in the reference to which the c2 reference points.

on line 3, the c1 reference variable now points to the same object as c2.
on line 4, the c2 reference variable now points to null, but c2 still points to the object.
on line 5, we return the value in c2, which is null...
so c3 points to null (back to line 10).

on line 11, c1 is made to point to null. Therefore object created on line 3 has no active references and is eligible for gc.
when we get to line 12, c2 still points to the object it originally did, so that object is NOT eligible for gc.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Thank you, Rob. I hadn't noticed the other thread.

It was posted after your request for the source.
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain the following lines

on line 10, we call the go() method of c1, and pass in the reference to which the c2 reference points.

on line 3, the c1 reference variable now points to the same object as c2.
on line 4, the c2 reference variable now points to null, but c2 still points to the object.

How does the c1 reference point to the same object as c2?

Kind Regards.

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to correct few things:

fred rosenberger wrote:on line 3, the c1 reference variable now points to the same object as c2.

cb and c2 both point to the object created in line 9

fred rosenberger wrote:on line 4, the c2 reference variable now points to null, but c2 still points to the object.

cb points to null, but c2 still points to the object created in line 9.

fred rosenberger wrote:on line 5, we return the value in c2, which is null...

We return the value in cb, which is null.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Campbell Ritchie wrote:Thank you, Rob. I hadn't noticed the other thread.

It was posted after your request for the source.
Are you telling me I can't see a thread before it is posted??
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohamed sanaullah wrote: . . . We return the value in cb, which is null.
As fr as I can see, and you have already been told this, that reference has always pointed to null, so there has never been an object to delete from memory.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Rob Prime wrote:
Campbell Ritchie wrote:Thank you, Rob. I hadn't noticed the other thread.

It was posted after your request for the source.
Are you telling me I can't see a thread before it is posted??

Well, YOU might be able to
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: . . . Well, YOU might be able to
I knew you would say that
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic