aspose file tools*
The moose likes Beginning Java and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

Edward Lawrence
Greenhorn

Joined: Apr 02, 2010
Posts: 21
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?



Regards,
Edward
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Please always tell us where the question is from.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

thread with the source


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Thank you, Rob. I hadn't noticed the other thread.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

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

Joined: Jun 21, 2008
Posts: 100

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

Joined: Sep 08, 2007
Posts: 3097
    
  33

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.

Mohamed Sanaulla | My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 27, 2005
Posts: 19784
    
  20

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
Rob Prime wrote: . . . Well, YOU might be able to
I knew you would say that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection