Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

GC help

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Self test (Kathy Sierra Ch3 :Q1) :-

Q:
When //doStuff is reached how many objects are eligible for GC ?

----I was wondering why only c1 & it's Short object are eligible for GC ....c3 is assigned a null reference via c1.go(c2){c2=null}....won't that be eligible as well ??
please help...Thanks in advance...
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to javaranch Raksha. Plese post your code inside the CODE tags. That will make it easier for people to read your post and thereby increase the chances of you getting a good response
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UseCodeTags when posting your coding. You don't create any object for variable c3. It's just a variable. So it isn't eligible for GC!

And Welcome to JavaRanch!~
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we are assigning null to c3 but before that c3 was not referring to some object
you already considered that the c3 object was referring to some object
Remember the c3 is mere a reference variable of the type CardBoard,
the variable has not been assigned some object

if we had written like this

then your assumption would have been right
the object referred by c3 would have been eligible for garbage collection because by making c3 null
the reference to the object through c3 is lost

merely creating a reference variable and assigning null value to it neither creates object nor makes some object eligible for GC
hope this helps

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is c1 and it's short variable eligible for garbage collection ?

an object is eligible for GC,if there is no more references to it.
new CardBoard();
can still be accessed using c2
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:why is c1 and it's short variable eligible for garbage collection ?

an object is eligible for GC, if there is no more references to it.


Let's put that coding in a Code Tag.


After Line 01, can you access the Object which we've created in the Line Ob 01?
 
Raksha Mishra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot everybody...
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

mohitkumar gupta wrote:why is c1 and it's short variable eligible for garbage collection ?

an object is eligible for GC, if there is no more references to it.


Let's put that coding in a Code Tag.


After Line 01, can you access the Object which we've created in the Line Ob 01?





when line 01 is removed how many object are eligible for garbage collection.........................
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

phil sohan wrote:
when line 01 is removed how many object are eligible for garbage collection.........................



Nothing will be eligible for Garbage Collection...
 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


class CardBoard{
Short story = 200 ;
CardBoard go(CardBoard cb) {
cb = null ;
return cb ;
}
public static void main(String [] args){
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2) ;
c1 = null ;
// do stuff
}
}



So there are two objects eligible for garbage collection one is c1 and
other one is instance Short variable story of CardBoard class
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the same nagging concern when I reviewed the answer to this question. I'm glad that I decided to search this forum and found this posting.

I was under the belief that "null" is a value when in comes to a reference variable. After reviewing the responses that the kind folks placed into this posting, I looked back in Chapter 3 of the K&B book. Take a look at the first line item in the chart at the bottom of page 193 as well as the text on page 195, and there is the answer. While the code:
returns null, as stated in the text, "A null value means the reference variable is not referring to any object on the heap." So C3 does not qualify for GC.

Thank you Java Ranch!

G
 
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