text: *
  A friendly place for programming greenhorns!
  Big Moose Saloon
  Search
  |
  Java FAQ
  |
  Recent Topics
  |
  Flagged Topics
  |
  Hot Topics
  |
  Zero Replies
  Register / Login
  JavaRanch
  »
  Java Forums
  »
  Certification
  »
  Programmer Certification (SCJP/OCPJP)
  Author
  Problem with GC exercises
  Federico Er
  Greenhorn
  Joined: Jun 22, 2010
  Posts: 8
  posted
  Jul 22, 2010 14:00:38
  0
  Hi all,
  i've been really bothered by these to exercises (10 and 11 from the book's third chapter), i just can't get it.
  class Dozenz { int [] dz = {1,2,3,4,5,6,7,8,9,10,11,12}; } public class Eggs { public static void main(String[] args) { Dozens [] da = new Dozens[3]; da[0]=new Dozens(); Dozens d = new Dozens(); da[1]=d; d = null; } }
  The answer is that 5 objets were created and 2 are elegible for GC, being "the second Dozens object (and its "int array" object" the ones that are not reachable. But instantiating a Dozens object does not create the int array, that's just an instance variable. Im i right? I know i must be wrong, but this is bothering me, i can't figure out why the int array counts as a reachable object here.
  And the next exercise
  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; } }
  In this case, there is only 1 object elegible for GC, because "there is still a reference to a2 (sure) and a2.b2 (sure!)", but here is the thing, "What might be less clear is that you can stil access the other Beta object through the static variable a2.b1 -because it's static". So a2.b1 it's static, but what does it have to do with a1 and b1, they where both set to null!
  Sorry to bother, it seems i can't quite grasp this subject, if someone can give me a hand or at least point me to a good garbage collection tutorial i will be really thankfull.
  Prateek Rawal
  Ranch Hand
  Joined: Dec 19, 2009
  Posts: 90
  posted
  Jul 22, 2010 15:55:25
  0
  An object becomes eligible for GC if it is not referenced by any reference variable.
  Q.10 you have written incomplete, there is one more line:
  da[1] = null;
  See, each Dozen object has a reference variable to an int array object containing elements 1,2,3.....so on
  Thus when you create a Dozen object, an int array object containing elements 1,2,3...so on also get created and the reference to that array object lies in the dozen object as 'dz'.
  Had the definition of class Dozens would have been something like this
  class Dozens { int[] dz;
  then when you create a dozen object, no array object would have been created, ofcourse there would be an int array reference in each Dozen object but it would have been initialized to null(Default value of object-type instance variable).
  So, now i hope you might have understood how 5 objects were created.
  Now as da[1] = null and d = null
  the second Dozen object becomes unreachable, and as it becomes unreachable, the array object pointed by reference variable inside it also becomes unreachable....
  Thus 2 objects become unreachable and thus eligible for GC
  Q.11)
  See, imagine a class like this:
  class UnderstandStatic { static int num; }
  Now when i create an object of this class,
  UnderstandStatic u1 = new UnderstandStatic();
  it has a primitive variable num with value 0(default value),
  u1.num--->0
  when i create another object of this class
  UnderstandStatic u2 = new UnderstandStatic();
  and write something like:
  u2.num = 5;
  Now when i print u1.num, what do you think would get printed?
  5 will get printed.
  A static variable is a class variable. It is shared by all objects of the class.
  Now coming back to your question,
  when we have written
  a1.b1 = b1;
  we tell Class Variable b1 to point to that b1 object.
  Now a2.b1 would also point to b1 object.
  More precisely there is no a1.b1 and a2.b1, there is just an object reference variable of type Beta named as b1 and it is pointed to a Beta Object.
  Thus a1.b1 and a2.b1 point to the same object.
  Now, even if we can't access that object by b1 and a1.b1, we can still refer it by a2.b1, thus the object is reachable and thus not eligible for GC,,,
  Hope you might have understood.......
  Waiting for enlightenment....
  Federico Er
  Greenhorn
  Joined: Jun 22, 2010
  Posts: 8
  posted
  Jul 22, 2010 17:08:48
  0
  Prateek, THANKS!
  You were very clear in your explanation in both exercises,
  so i understand both issues now,
  thanks!!
  Prateek Rawal
  Ranch Hand
  Joined: Dec 19, 2009
  Posts: 90
  posted
  Jul 22, 2010 17:45:04
  0
  Refer this
  image
  link:
  GC Graphical View
  This will give you a very clear picture,
  See variable b1 of each Alpha object(a1 and a2),being static it is shared.
  Now consider each of the objects(1,2,3,4)
  As a1 = null
  we can't access (4) thus it is eligible for GC,
  Now b2 = null,
  but we can access (2) through a2.b2, thus it is not eligible for GC
  Now (3) is still accessible as a2 is not null, thus it is also not eligible for GC
  Now see (1), although b1 = null and a1 = null,
  it can't be accessed through a1.b1 and b1,
  but it can still be accessed by a2.b1, thus it is also not eligible for GC
  Thus only single object is eligible for GC i.e. (4)
  Happy
  SCJP
  !
  I agree. Here's the link:
  subject: Problem with GC exercises
  Similar Threads
  Garbage Collection
  Garbage Collection Problem
  GC question
  Error in the SCJP6 Study Guide Mock Exam Question?
  Objects eligible for GC
  All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
  JForum
  |
  Paul Wheaton