Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

about Garbage Collection

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code:

Class CardBoard{
Short story=5;
CardBoard go(CardBoard cd){
cd=null;
return cd;}
public static void min(String ags[]){
CardBoard c1= new CardBoard();
CardBoard c2=new CardBoard():
CardBoard c3= c1.go(c2);
c1=null;
}
}

now at the end 2 objs are eligible for gc..Can any one explain..

Thanks in advance..
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arun,
i think like all the three ref varibales are eligible for GC.
1.c2=null reason: you are making it null when method executes
2.c3=null since you are assinging method(go()).
Cardboard c3=c1.go(c2);
3.c1=null;
Regards
Kirba.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who knows? The code wouldn't even compile, because the keyword "class" shouldn't be capitalized, and although the previous poster mentions three CardBoard objects, I only see two being created. And are the "Short" wrapper objects supposed to be counted?

Please confine SCJP mock questions to the SCJP forum; they're not really language questions and often, like this one, they don't make any sense.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of it this way:

Think of your hand as a reference variable to a class of type Kite.

You are flying a kite with the left hand, drinking an iced tea with the right. Someone walks up and holds out the string to another kite. Your left hand lets go of the old kite and grabs the new kite. Here's what you have....

1) the "kite reference variable" takes on the string(reference) to the new kite so that the NEW kite is now referenced to you.

2) the old kite, having lost it's reference, now HAS no reference and no one claiming it.... so it will more than likely be picked up by a garbage collector SOMEWHERE!!!

On a serious note the above example is like instantiating a new object and passing it's reference to a reference variable that WAS pointing to another object. Now, the old object has nothing pointing to it so it is now elligible for garbage collection.

The other case is assigning null to a reference variable that WAS pointing to an object. The outcome is exactly the same.... the old object has nothing pointing to it... so it is now elligible for garbage collection.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arun,

you should have a look at the errata, since your example is wrong.

The corrected question:Now there are two objects eligible for garbage collection.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arun

I recall coming across this question in the Kathy Sierra and Bert Bates SCJP book.

The only Cardboard object that is eligible for garbage Collection is c1, but it contains a Short Object, which will also now become eligible for garbage collection.

c2 is still a valid object and c3 never had the new operator called, so it never referenced any object.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic