• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Garbage collection

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Marshal
Posts: 74044
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always tell us where the question is from.
 
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thread with the source
 
Campbell Ritchie
Marshal
Posts: 74044
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Rob. I hadn't noticed the other thread.
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74044
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74044
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74044
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote: . . . Well, YOU might be able to

I knew you would say that
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic