This discussion is archived
7 Replies Latest reply: Nov 6, 2012 7:29 PM by 887176 RSS

Question on Garbage collection

887176 Newbie
Currently Being Moderated
class Beta { }
class Alpha {
static Beta b1;
Beta b2;
}
public class Tester {
public static void main(String[] args) {
Beta b1 = new Beta(); Beta b2 = new Beta();
Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
a1.b1 = b1;
a1.b2 = b1;
a2.b2 = b2;
a1 = null; b1 = null; b2 = null;
// do stuff
}
}
When line 16 is reached, how many objects will be eligible for garbage collection?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 5

What should be the answer?.
according to me it should be a1,b1,b2 and a2....so 4 objects?
can anyone explain how many objects should be collected by GC?
  • 1. Re: Question on Garbage collection
    EJP Guru
    Currently Being Moderated
    Why a2?
  • 2. Re: Question on Garbage collection
    887176 Newbie
    Currently Being Moderated
    Ya i looked at it now a2 should not be part of garbage collection, here a2.b2 = null does not mean a2 is null....so basically 3 objects should be garbage collected.
    but actual answer is 1.i.e

    i did not understand how only 1 object is eligible for GC.....
  • 3. Re: Question on Garbage collection
    rp0428 Guru
    Currently Being Moderated
    >
    When line 16 is reached, how many objects will be eligible for garbage collection?
    >
    All of them - you only posted 16 lines of code so execution is completed.

    Assuming you mean that there really is more code somewhere then why do you say
    >
    here a2.b2 = null does not mean a2 is null
    >
    When did a2.b2 become null? I don't see any code where that happens.

    I think you are confusing 'objects' and variables (references to objects). Objects are created on the heap, variables (references) are created on the stack. The garbage collector works with the heap.

    So post your answer to these questions

    1. How many 'objects' are created on the heap?

    2. How many instance variables are defined in the main method?

    3. Is b2 (declared in the main method) an object on the heap? Or is it an instance variable which is a reference to an object on the heap?

    4. Is a2.b2 an object? Or is it an instance variable?

    5. What does the assignment a2.b2 = b2 mean? Does it mean that a2.b2 references b2? Or does it mean that a2.b2 references the object on the heap that b2 points to?

    6. If TWO instances of class Alpha exist and the static Beta b1 is assigned a value does that value disappear if only one of the Alpha instances is removed? In other words what effect does declaring Beta b1 as static have?
  • 4. Re: Question on Garbage collection
    887176 Newbie
    Currently Being Moderated
    Yes i think i am confused between objects and variables and the concept of heap and stack.

    There are only 16 lines of code which was given in question

    a2.b2 never becomes null, since in a2.b2 = b2, and b2 is null so i assumed a2.b2 is null as well.


    Answers to below questions

    1. How many 'objects' are created on the heap?
    Answer: 4 objects - a1,a2,b1,b2

    2. How many instance variables are defined in the main method?
    Answer : Three instance variables in main method.a1.b1, a1.b2, a2.b2.

    3. Is b2 (declared in the main method) an object on the heap? Or is it an instance variable which is a reference to an object on the heap?
    Answer: Yes b2 is an object on heap

    4. Is a2.b2 an object? Or is it an instance variable?
    Answer: It is an instance variable

    5. What does the assignment a2.b2 = b2 mean? Does it mean that a2.b2 references b2? Or does it mean that a2.b2 references the object on the heap that b2 points to?
    Answer: It mean that a2.b2 references b2 variable

    6. If TWO instances of class Alpha exist and the static Beta b1 is assigned a value does that value disappear if only one of the Alpha instances is removed? In other words what effect does declaring Beta b1 as static have?
    Answer : static b1 will be tied to class instead of object....so removing Alpha instance will no effect on b1.


    since a1=null, b1=null and b2=null....it definitely means that these three objects are null and hence can be garbage collected.

    so is correct answer 3?.But actual answer is 1 which i am not getting how.

    Its a question from Kathy Sierra book which says :-
    B is correct. It should be clear that there is still a reference to the object referred to by
    a2, and that there is still a reference to the object referred to by a2.b2. What might be
    less clear is that you can still access the other Beta object through the static variable
    a2.b1—because it's static.

    Pls help me understand this....i think i am missing some basic concept.
  • 5. Re: Question on Garbage collection
    rp0428 Guru
    Currently Being Moderated
    >
    Pls help me understand this....i think i am missing some basic concept.
    >
    Yes - you are. Since you were wrong about almost every question I asked.
    >
    1. How many 'objects' are created on the heap?
    Answer: 4 objects - a1,a2,b1,b2
    >
    Care to try again? This was the code
    Beta b1 = new Beta(); Beta b2 = new Beta();
    Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
    The objects are a new Beta, another new Beta, a new Alpha and another new Alpha. Those are the objects created on the heap.
    >
    2. How many instance variables are defined in the main method?
    Answer : Three instance variables in main method.a1.b1, a1.b2, a2.b2.
    >
    Care to try again? b1, b2, a1 and a2 are the four local variables declared in the main method. They reference the four objects that were created by the 'new' operator (see question #1).

    Object types are created on the heap. Variables are created on the stack.
    >
    3. Is b2 (declared in the main method) an object on the heap? Or is it an instance variable which is a reference to an object on the heap?
    Answer: Yes b2 is an object on heap
    >
    See explanation for #2 above and try again.
  • 6. Re: Question on Garbage collection
    887176 Newbie
    Currently Being Moderated
    1. How many 'objects' are created on the heap?
    Answer: 4 objects - 2 alpha and 2 beta

    2. How many instance variables are defined in the main method?
    Answer : Three instance variables in main method.a1, a2, b1, b2.

    3. Is b2 (declared in the main method) an object on the heap? Or is it an instance variable which is a reference to an object on the heap?
    Answer: b2 is instance variable on stack pointing to object on Heap

    4. Is a2.b2 an object? Or is it an instance variable?
    Answer: It is an instance variable

    5. What does the assignment a2.b2 = b2 mean? Does it mean that a2.b2 references b2? Or does it mean that a2.b2 references the object on the heap that b2 points to?
    Answer: It mean that a2.b2 references b2 variable

    6. If TWO instances of class Alpha exist and the static Beta b1 is assigned a value does that value disappear if only one of the Alpha instances is removed? In other words what effect does declaring Beta b1 as static have?
    Answer : since b1 is static n is tied to class which is loaded already....it will refrain alpha object to get garbage collected.so both a1 will not be garbage collected.
    a2 is anyways not being collected.

    so remaining objects are b1 and b2 which shud be garbage collected.

    Pls let me know if i am correct now.Trying to learn from this forum.
  • 7. Re: Question on Garbage collection
    rp0428 Guru
    Currently Being Moderated
    >
    Answer : Three instance variables in main method.a1, a2, b1, b2.
    >
    So when you count a1, a2, b1, b2 you count 'three instance variables'?

    Try taking of your left shoe and sock and count on your toes to see how many you get.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points