aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Eligible for garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Eligible for garbage collection" Watch "Eligible for garbage collection" New topic
Author

Eligible for garbage collection

Quirino Gervacio
Greenhorn

Joined: Aug 09, 2008
Posts: 23
From Sierra and Bates SCJP6 Study Guide
Chapter 3: Self Test, Question 11, Page 275


According to the book, when line 16 is reached, there is only one object eligible for garbage collection. I really don't know why. I always come up with 2 objects eligible for garbage collection which are the object refered to by b1 and a1. Please help.


SCJP 6.0
Faber Siagian
Ranch Hand

Joined: Jul 08, 2008
Posts: 52
I think a1 is the only one object which is eligible for GC.

Alpha's b1 is static, means it's a class variable, not an instance variable.
So since b1 is assigned to a1.b1, a2.b1 still refers to b1.

But i'm not so sure about this, correct me if i'm wrong.


Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0 (88 %)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

a1 is eligible for garbage collection...you are right in that....

but b1 is not eligible for garbage collection.....

If you have noticed in the code

class Alpha {
static Beta b1;
Beta b2;
}

see the first reference variable is static. this means that it is not associated with any object of class Alpha.

let me modify the code to make it more understandable


Now according to you the object referenced by beta1 must be eligile for garbage collection. but it will be not..at (1) beta1 is stored into b1 of Alpha class. b1 is static in class Alpha. so at (2) when a1 is set to null it will have no effect on b1.

I hope you will get it..... :roll:


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
Originally posted by Quirino Gervacio:
From Sierra and Bates SCJP6 Study Guide
Chapter 3: Self Test, Question 11, Page 275


According to the book, when line 16 is reached, there is only one object eligible for garbage collection. I really don't know why. I always come up with 2 objects eligible for garbage collection which are the object refered to by b1 and a1. Please help.


Dear Quirino,

I believe the key to this question is the static Beta b1; in class Alpha.

Beta b1 = Beta();(1)
Beta b2 = Beta();(2)
Alpha a1 = new Alpha();(3)
Alpha a2 = new Alpha();(4)
Creates 4 objects on the heap. So these are the possible choices for gargabe collector.

a1.b1 = b1; This creates a reference to object created at line 1. You know that static variables are created only once for that class and all the instances of that class share them. So even when there is no instance of the class, the static variables remain intact. That means even a1 is made null. There is still a valid reference to the object created at line 1.

So we eliminate object created at line 1.

a1.b2 = b1; this will null as soon as a1 is made null as it's instance variable

a2.b2 = b2; this line will maintain references to 2 objects created at line 2 and 4 as a2 is not set to null at any line of code.

So only object created at line 3 is eligible for garbage collection.

Hope my explanation is not so confusing.
Quirino Gervacio
Greenhorn

Joined: Aug 09, 2008
Posts: 23
Alas!!! i finally got it!

The answer is really one object eligible for GC and that object is a1.

I forgot that a1.b1 is misleading. It is actually Alpha.b1

Many thanks!
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
Originally posted by Quirino Gervacio:
Alas!!! i finally got it!

The answer is really one object eligible for GC and that object is a1.

I forgot that a1.b1 is misleading. It is actually Alpha.b1

Many thanks!


Allow me to correct your sentence. The "object" is created by "new". Not a reference. In this case a1 or a1.b1 is reference to an object. NOT object.

the objects on the heap here are new Alpha() or new Beta()
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Hello !!!
Quirino ,can you send me your K&B SCJP 6 e-book ,in my place it is unavailable and I am preparing for SCJP 6 my e-mail ID is :
ashokpradhan_61@yahoo.com.Thanks!!!
Quirino Gervacio
Greenhorn

Joined: Aug 09, 2008
Posts: 23
Hi Ashok,

I would love to but I'm not sure if I'm allowed to send copy of the e-book version of the book.

Perhaps we could ask the moderator of this thread.
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

friend it is your choice you send it or not .I am not supposed to use it commercially only for reference and to pass the scjp 6 exam and if one of your friend want your book ,do you ask the moderator for it ?In my town it is not not available,so I can not buy it,if it come to me town then I will buy it,but it would be late for me,I want to schedule my exam within 3 weeks.Thank you friend .and I am waiting .. for moderator's replay.At last everything is depend upon you.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hi Ashok,

It's possible that everything you've said in this thread makes sense - but it's confusing to me so perhaps you can help me out!

Apparently you are studying for the SCJP exam, and yet you live in a place where they can't mail you a book? Okay, maybe that's possible, where is it?

Now generally you could say that that's none of my business, and typically you'd be correct, but in this case you are asking for a free copy of material that I spent months working to create.

In any case, I appreciate you asking the moderators for permission, and I can say that the general rule is that JavaRanch cannot allow the illegal distribution of copyrighted material.

I think the next step is for you to explain your special situation more clearly.

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Thanks Bert,
I am waiting for you ,and I am a Fan of K&B I have Head First Java,Head First Servlet and JSP ,and I also SCJP 5 guide by you and Kathy.
Before I had bought all these books I had all the e-books but, I still bought these books from a bookstore .Why I am spending money though I had e-books ??
When the book is available in my market I must buy it,but for now and for the exam point of view it is not possible ,if I buy it from internet ,it may be late for me when I got it(not guaranted like thread behavior) may be I passed the exam ,so it might not help me.At last I think the distribution of copyright materials are not illegal,but we human beings make it illegal.

Thanks !!!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hi Ashok,

It's great news that you have the SCJP 1.5 book! We really recommend that if you already have the 1.5 book, you DO NOT need to buy the 1.6 book! The difference between the two exams is very small (and has been documented here many times.) The difference between the two books is very small except for about 75 new mock questions. As much as I'm proud of my mock questions I don't think 70 questions is worth buying a whole new book!

In other words - between the book you already have, and the information at the ranch about a few small new topics on the SCJP 6 exam - you ALREADY HAVE everything you need!

hth,

Bert
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Thanks Bert

The difference between the two exams is very small (and has been documented here many times.) The difference between the two books is very small except for about 75 new mock questions. As much as I'm proud of my mock questions I don't think 70 questions is worth buying a whole new book!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Eligible for garbage collection