# Bert Bates Kathy Sierra Java 6 Study Guide

Paul Wam
Greenhorn
Posts: 20
Garbage Collection has me scratching my head a few times. Maybe you can explain - Chapter 3, question #10 reads

Which two are true about the objects created within main(), and eligible for garbage collection when line 14 is reached?

A. Three objects were created
B. Four objects were created
C. Five objects were created
D. Zero objects are eligible for GC
E. One objects are eligible for GC
F. Two objects are eligible for GC
G. Three objects are eligible for GC

The book says
C and F are correct. da refers to an object of type "Dozens array" and each Dozens object that is created comes with its own "int array" object. When line 14 is reached, only the second Dozens object (and its "int array" object) are not reachable.

I thought B was correct (and not C) because for every new Dozens object, two objects are created (dozens and int array). There are two new Dozens objects (da[0] and d) thus 4 objects. I did not count da[1] because it points/refers to an object that already exists, d. Should I have...if so, why?

W. Joe Smith
Ranch Hand
Posts: 710
You are right about every new Dozens creating two new objects, a Dozen and an int array. Since 2 Dozens are created, you get 4 objects. PLUS: The Dozens array. It is also an object, making 5 objects.

Minhaj Mehmood
Ranch Hand
Posts: 400
Paul Wam wrote:I thought B was correct (and not C) because for every new Dozens object, two objects are created (dozens and int array). There are two new Dozens objects (da[0] and d) thus 4 objects. I did not count da[1] because it points/refers to an object that already exists, d. Should I have...if so, why?

basically arrays are it self objects.
and when we say it means there are 2 objects are created, 1 object type of Dozens() 2nd object type of int array which Dozens class contains.
here it means you GCed 2 objects 1 the object of Dozens class itself and its int type of array.

HTH

W. Joe Smith
Ranch Hand
Posts: 710
M M kaimkhani wrote:
here it means you GCed 2 objects 1 the object of Dozens class itself and its int type of array.
HTH

Techincally, they just became eligible for GC at this point. We don't really know when (or even if) the GC will gather them before the end of the program.

Paul Wam
Greenhorn
Posts: 20
Thanks...I didn't catch that the array, da, is an object separate from the objects in its indexes or indeces.

Weiheng Zhang
Greenhorn
Posts: 9

gc illustration.gif

j dam
Greenhorn
Posts: 9
i hope this helps.
exm.jpg

Mehdi Ben Larbi
Ranch Hand
Posts: 70
I am sorry,but i didn't understand :

Why isn't the d[1] eligible for GC,it's null reference too ?

Abimaran Kugathasan
Ranch Hand
Posts: 2066
Mehdi Ben Larbi wrote:I am sorry,but i didn't understand :

Why isn't the d[1] eligible for GC,it's null reference too ?

d[1] is a reference, not an object. So how is it eligible for GC?

Mehdi Ben Larbi
Ranch Hand
Posts: 70
Abimaran Kugathasan wrote:
d[1] is a reference, not an object. So how is it eligible for GC?

So references to Array Objects ( d[0],d[1] ... ) are never eligible for GC ?
I am really confused with GC,i am having a lot of trouble understanding it.
Do you have any good study material for the exam please ?