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

Confused with Garbage Collectible Objects

 
Vince Tan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm looking at this Chapter 3 Self Test in K&B's book asking about how many objects that are eligible for garbage collection. Following is the code:




The answer from the book is 3 objects. However, my view is that it should be 2.

Question 1:
On page 166, it's stated that local variables live on the stack instead of the heap. So, wouldn't the w1 and w2 live on the stack and won't be eligible for GC since it's not even in the heap?


Question 2:
The string literal within the print statement in the go() method would still contain reference to it from the String Literal Pool so it would not be eligible for GC, is this right? Just to confirm it would only create one object in the heap?


Question 3:
When the new Wind(3) object gets created in the main() method, does it create one object (the Wind) or does it creates 2 (the Wind and the int id within the object - unless primitive types are not considered an object? )?


Question 4:
I guess this is related to Q3, when an instance of a class gets created (an object with say 2 objects or classes and 1 primitive type), how many objects get created in the heap? I assume it should be just one as the containing Objects and primitive types should be part of this main object?


Thanks in advance.
 
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
Vince Tan wrote:Question 1:
On page 166, it's stated that local variables live on the stack instead of the heap. So, wouldn't the w1 and w2 live on the stack and won't be eligible for GC since it's not even in the heap?

Be careful! A variable is not the same as an object. w1 is indeed a local reference variable and lives on the stack. But the object it's refering to (a Wind instance with id=1) lives on the heap (and thus will be eligible for GC once the method has finished executing.

Vince Tan wrote:Question 2:
The string literal within the print statement in the go() method would still contain reference to it from the String Literal Pool so it would not be eligible for GC, is this right? Just to confirm it would only create one object in the heap?

True! It's a String literal, a compile time constant. So the string " " is added to the String Literal Pool and will not be eligible for GC (because the pool still refers to this string).

Vince Tan wrote:Question 3:
When the new Wind(3) object gets created in the main() method, does it create one object (the Wind) or does it creates 2 (the Wind and the int id within the object - unless primitive types are not considered an object? )?

A primitive is NEVER an object! Never! I'll repeat it once more: never! That's one of the main reasons why Java isn't considered to be a pure OOP language. So new Wind(3) creates just 1 object. (Note: it would be different if the type of id was Integer instead of int).

Vince Tan wrote:Question 4:
I guess this is related to Q3, when an instance of a class gets created (an object with say 2 objects or classes and 1 primitive type), how many objects get created in the heap? I assume it should be just one as the containing Objects and primitive types should be part of this main object?

Incorrect! If the instance members are initialized instantly (or in the constructor or in the instance initializer block), the other objects will be created on the heap as well. Primitive instance members are not objects as you know and thus will not be objects eligible for GC (but will exist on the heap as well).

Given this code snippet:How many (and which) objects will be created if you call new Kitchen(5, 32); (in a main method for example)?

[edit] Answer the question first! Then you can scroll down to the end of this thread (where you can find the correct answer).

Hope it helps!
Kind regards,
Roel
 
Mateus Brigido
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Given this code snippet:How many (and which) objects will be created if you call new Kitchen(5, 32); (in a main method for example)?

Hope it helps!
Kind regards,
Roel


I'm studying to the exam too, so i'll try to answer this!

One Kitchen object
Five Chairs objects
One Table object

Surface is a primitive and stove is not initialized in the constructor or in an initializer block

Is this right?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mateus Brigido wrote:One Kitchen object
Five Chairs objects
One Table object

Is this right?

Close, but no cigar!
 
Mateus Brigido
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Mateus Brigido wrote:One Kitchen object
Five Chairs objects
One Table object

Is this right?

Close, but no cigar!


One ArrayList Object, just forgot that one
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mateus Brigido wrote:
Roel De Nijs wrote:
Mateus Brigido wrote:One Kitchen object
Five Chairs objects
One Table object

Is this right?

Close, but no cigar!


One ArrayList Object, just forgot that one

Much better! (note: object should be with lowercase o)
 
Vince Tan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
8 Objects! Thanks so much for the explanation! One last question:



I understand that " " will be in the heap with a referenced from the String Literal Pool. However, when the entire string gets generated as in "1 2" will this string gets stored in the heap the same way as the " " ? Or it doesn't because it's for printing but the following will generate 2 objects?




Will the above creates 2 objects in the heap?
String object 1 -> " "
String object 2 -> "1 2"

 
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
Vince Tan wrote:I understand that " " will be in the heap with a referenced from the String Literal Pool. However, when the entire string gets generated as in "1 2" will this string gets stored in the heap the same way as the " " ?

Only String literals (and compile time constants) will have a reference in the String Literal Pool, a string created at runtime (like "1 2") will just be created on the heap, and will not be referenced from String Literal Pool. Here you'll find an excellent article about strings and the String Literal pool. Definitely a must read!

Vince Tan wrote:Will the above creates 2 objects in the heap?
String object 1 -> " "
String object 2 -> "1 2"

No! 3 objects will be created:
1/ " "
2/ "1 " (a temporary String after concatenating w1.id and " ")
3/ "1 2" (concatenating the temporary string "1 " and w2.id)

Hope it helps!
Kind regards,
Roel
 
Vince Tan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the great article, I actually read that from the link you posted on another thread.

So just to sum it up, based on the strings generated by this statement System.out.println(w1.id + " " + w2.id);

1/ " " <-- is this considered a string literal and will be referenced by the pool hence NOT eligible by GC?
2/ "1 " <-- generated on the fly hence will be in the heap with no reference from the pool
3/ "1 2" <-- generated on the fly hence will be in the heap with no reference from the pool

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vince Tan wrote:1/ " " <-- is this considered a string literal and will be referenced by the pool hence NOT eligible by GC?
2/ "1 " <-- generated on the fly hence will be in the heap with no reference from the pool
3/ "1 2" <-- generated on the fly hence will be in the heap with no reference from the pool

True! " " is indeed a String literal and will have a reference from the String Literal Pool, so it's not eligible for GC. Both other strings are created at runtime, so not referenced from the String Literal Pool.
 
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
Regarding this discussion (and your doubts), this note is quite an important one (from K&B7, page 201)
K&B7 wrote:Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a look at question 7 of the Chapter 3 Self Test as well. Because of the temporary strings created by the print statement, answer choices D to G should explicitly refer to Wind objects to be really unambiguous and correct.

Added to the errata overview.
 
Vince Tan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel! That makes more sense now!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic