• 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
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Garbage Collection

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public 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); // # 1
c1 = null;
//doStuff
}
}

How many objects are created and how many objects are eligible for GC? And Also, I don;t understand what is actually happening at line # 1?
Can someone help me with this question, please?
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from Kathy Sierra & Bert Bates's book SCJP6 Study Guide Chapter 3 Question 1.
 
Ranch Hand
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janki Shah wrote:public class CardBoard {
Short story = 200;
CardBoard go(CardBoard cb)
{
cb = null; //# 0
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard(); //line a
CardBoard c2 = new CardBoard(); //line b
CardBoard c3 = c1.go(c2); // # 1
c1 = null;
//doStuff
}
}

How many objects are created and how many objects are eligible for GC? And Also, I don;t understand what is actually happening at line # 1?
Can someone help me with this question, please?



1. First of all, how many objects are created on line a and line b? I would say four, the cardboard on line a , its Short story object is autoboxed and the cardboard on line b and its Short story object.

2. What happens to line #1 ? c2 variable is passed to go method. c2 variable refers to the object created in line b. In the method, cb variable refers to the object created in line b. But cb then refers to null in line #0. The null reference is returned and therefore c3 refers to null. After line #1, nothing is eligible for GC. It is because c3 refers to null reference.

If I change the code a little bit like this:

After line #1 , the object created in line c will be eligible for GC because it was refered by c3 once. But at line #1, c3 variable refers to null.


3. Ok...after digesting my 1 and 2, let's go back to this example again:

When the line //do stuff is reached, c1 variable now refers to null. The object created in line a has no variable refering to it. So, it will be eligible for GC as well as its Short story object. So, 2 objects will be eligible for GC at this line.
The object created in line b is not eligible for GC because c2 variable is still refering to it.

4. When the main method finishes, the objects created in line b will be eligible for GC.

For the exam, pay attention to the difference between object created in a line and its variable reference. I think most people including me always get confused by these two terms.
The object created and its variable reference are two different things. As long as the object created has no variable refering to it, it will be eligible for GC.

 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Helen for a great explanation.
Still I have one doubt is that, when CardBoard c3 = c1.go(c2); finishes, will c2 be null? since go() method sets it's argument to null.
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a good exam question: Will c2 be null?
The answer is no.
Explanation:



cb refers to the object refered by c2. At line #0, cb refers to null. c2 still refers to the object created at line b.
Read the section from KB's book about pass by value.
Try to do this after the line //doStuff :

I am sure c2 is not null and c2 is still refering to the object created at line b.
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes c2 is not null, after System.out.print(c2);.
And it is very clear in Pass-By-Value topic that, "The called method can't reassign the caller's original reference variable and make it refer to a different object or null."

Thank you so much Helen.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic