Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collector question

 
Javier Giron
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!

Im preparing myself for the exam Java SE 7 Programmer I 1Z0-803. And i cant understand this question and his answer. I will write the question, the correct answer and my doubt.

Given:



When //do stuff is reached, how many objects are eligible for garbage collection?
A. 0
B. 1
C. 2
D. Compilation fails
E. It is not possible to know
F. An exception is thrown at runtime.

Answer wrote:C is correct. Only one Cardboard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.


Ok, so i understand why the Short item of c1 is eligible, thats not a problem.

If c1 is null and its eligible, why c3 isnt also eligible? because is also null. So in this case we would have 3 objects eligible to the GC, c1, Short item who was pointed by c1 and c3.

And also i dont understand the next. Why c2 isnt null at the end of the code? Because, as i understand, the Cardboard cb in the funtion go() is pointing to the same object as c2. So when the code says "cb = null;" isnt making c2 = null? (I know this is incorrect because i executed the code and isnt working like that, but i cant understand why)

So these are my questions, why c2 isnt null? why c1, which value is null, is eligible for the GC and c3, which value is also null, isnt eligible??

Thank you so much in advance!
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when you invoke this method:

you pass to parametr cb copy of link (adress of heap where actual object is reside) to this object (CardBoard c2 = new CardBoard();)
than inside method you asign to cb null and return it.
Example:

and than result of invocation you asign to CardBoard c3
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Javier Giron,

First of all, a warm welcome to the CodeRanch!

Let's start with the easiest one of your questions

If c1 is null and its eligible, why c3 isnt also eligible? because is also null.

c1 and c3 are both reference variables of type CardBoard. But there is a big difference. Important to remember: Only objects can be eligible for GC (not reference variables).

c1 is referring to a new CardBoard object/instance (line 12). When null is assigned to c1 on line 15, the object/instance (which was created on line 12 using new CardBoard()) will be eligible for GC.
As you have already discovered (from your original post) c3 is referring to null, just like c1. But when you declare a reference variable, you NEVER create an instance of that type. You can only create an object/instance when you use keyword new (as in lines 12 and 13). So c3 doesn't refer to an object/instance (it refers to null), therefore the object/instance can't be eligible for GC (because it doesn't exist).

A tip to help you with these kind of questions is to look at how many times the keyword new is invoked. That number is the number of how many objects that are created But be careful: with autoboxing (as in line 3) the new invocation is done behind the scenes. But line 3 actually reads: Short story = new Short((short)200);. So in this example there are 4 invocations of keyword new, so 4 objects created and thus at most 4 objects which can be eligible for GC at line 16.

And now a little pop-quiz: what if line 3 was changed to short story = 200;? How many objects will there be created now? Still 4?

Hope it helps!
Kind regards,
Roel

PS. When you understand this part, we'll get to your other question
 
Javier Giron
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh ok!!! i got it!! i was mixing the instance variables with the objects! Now, with your explanations and some drawings, everything is clear! Thank you so much to all!!

Roel De Nijs wrote:And now a little pop-quiz: what if line 3 was changed to short story = 200;? How many objects will there be created now? Still 4?


Just two objects, the CardBoard ones, because the short variable isnt a object anymore is a primitive variable, right?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javier Giron wrote:
Roel De Nijs wrote:And now a little pop-quiz: what if line 3 was changed to short story = 200;? How many objects will there be created now? Still 4?


Just two objects, the CardBoard ones, because the short variable isnt a object anymore is a primitive variable, right?

Spot-on!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And as promised we move on to your next question, which is a little harder than the 1st one but now we are warmed up

Javier Giron wrote:And also i dont understand the next. Why c2 isnt null at the end of the code? Because, as i understand, the Cardboard cb in the funtion go() is pointing to the same object as c2. So when the code says "cb = null;" isnt making c2 = null? (I know this is incorrect because i executed the code and isnt working like that, but i cant understand why)

Another important thing to remember: Java uses pass-by-value. And that's actually the reason why c2 isn't null. How this actually works is well explained (much better than I'll ever can) in this article (already mentioned by Sergej too). So just read the article(s) and if you still have doubts/questions, just post them here.
 
Javier Giron
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything clear, that article explains it in a very easy way. Thanks to all again! Very useful answers!
 
Sharmili Rameshbabu
Greenhorn
Posts: 27
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have few doubts regarding this topic. Can you please address them.

1. If the value of the instance variable story was in the range of -128 to 127, then there are no objects created for it. Right?
2. if line 1 is added in the below code , then object referred by story is not eligible for garbage collection. Right?


3. If line 1 is modified as short s=c1.story; does the behaviour is same as the previous one?
4. If a class has a static member variable, they are never eligible for garbage collection till the program terminates. Right? As in this example


Please correct me if am wrong, am writing this to ensure my understanding.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmili Rameshbabu wrote:I have few doubts regarding this topic. Can you please address them.

Yes

Sharmili Rameshbabu wrote:1. If the value of the instance variable story was in the range of -128 to 127, then there are no objects created for it. Right?

You are referring to the cached values. So when the 1st instance of CardBoard is created a new instance of Short (with value 200) will be created. On every next instantiation of CardBoard, this instance will be used (and thus no others will be created).

Sharmili Rameshbabu wrote:2. if line 1 is added in the below code , then object referred by story is not eligible for garbage collection. Right?

Correct!

Sharmili Rameshbabu wrote:3. If line 1 is modified as short s=c1.story; does the behaviour is same as the previous one?

No, it isn't. The Short object (to which c1.story is referring to) will be unboxed to a short, and because there is no reference to this Short object anymore, it will be eligible for GC when //do stuff is reached.

Sharmili Rameshbabu wrote:4. If a class has a static member variable, they are never eligible for garbage collection till the program terminates. Right? As in this example

Not true! Your code can assign null to a static class reference variable and then the object (if it has no other reference variables pointing to it) will be eligible for GC.

Hope it helps!
Kind regards,
Roel
 
Sharmili Rameshbabu
Greenhorn
Posts: 27
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for quick response.

Roel De Nijs wrote:
Sharmili Rameshbabu wrote:1. If the value of the instance variable story was in the range of -128 to 127, then there are no objects created for it. Right?

You are referring to the cached values. So when the 1st instance of CardBoard is created a new instance of Short (with value 200) will be created. On every next instantiation of CardBoard, this instance will be used (and thus no others will be created).


(with value 200)??? I meant to ask if it was say Short story=1; then there are no objects created in heap, since they are created in cache and referred by all the instances of the CardBoard, So answer to the question would be just 1 object(c1) eligible of garbage collection. Correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmili Rameshbabu wrote:(with value 200)??? I meant to ask if it was say Short story=1; then there are no objects created in heap, since they are created in cache and referred by all the instances of the CardBoard, So answer to the question would be just 1 object(c1) eligible of garbage collection. Correct?

That was of course a typo of mine. I was a little bit too hasty

Yes! Even if all CardBoard instances are eligible for GC, this Short instance will still be referenced from the Short cache, so it won't be eligible for GC.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic