Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection with immutable short.

 
jamil lusa
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all from the K&B book chapter 3 question 1 regarding the garbage collection:






if i change the value "Short story = 200" to lower than 127, then what will be the answer? Since the Short value will become immutable, and it will reside back to "pool".
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shorts are always "immutable". "Immutable" means "unchangeable". What happens when you change the value from 200 to 127 is that the auto-boxed Short is pooled in the cache. So since it's pooled in the cache, it won't be eligible for garbage collection.
 
jamil lusa
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaydell Leavitt wrote:Shorts are always "immutable". "Immutable" means "unchangeable". What happens when you change the value from 200 to 127 is that the auto-boxed Short is pooled in the cache. So since it's pooled in the cache, it won't be eligible for garbage collection.


Shorts is always "immutable" only when the value lay in the range of -128 to 127?




so if the value change to lower than 127 then the answer would be B?

 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shorts is always "immutable" only when the value lay in the range of -128 to 127?


Definition of Immutable
 
jamil lusa
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaydell Leavitt wrote:

Shorts is always "immutable" only when the value lay in the range of -128 to 127?


Definition of Immutable


ok thanks now i understand the definition, but what is the answer for the question in these 2 cases?
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think reference variable is created which is pointing to the 200 in pool not a OBJECT since , there is no new () operator involved. So Short wouldn't be eligible for GC.


As for the question of this thread has post , C is correct ans. As c1 and cb within go() is eligible for GC. so count is 2




 
John Stark
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think reference variable is created which is pointing to the 200 in pool not a OBJECT since , there is no new () operator involved. So Short wouldn't be eligible for GC.

It is a Short object with an associated value of 200. The value of 200 on the right side is a primitive which is autoboxed. The new object is created in this autobox process. So a new Short object is created even if you don't do

As c1 and cb within go() is eligible for GC. so count is 2

The go() method is invoked on the object c1 is referencing, but c1 is not changed in this method so the corresponding object will not be eligible for GC.

John
 
jamil lusa
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Stark wrote:
I think reference variable is created which is pointing to the 200 in pool not a OBJECT since , there is no new () operator involved. So Short wouldn't be eligible for GC.

It is a Short object with an associated value of 200. The value of 200 on the right side is a primitive which is autoboxed. The new object is created in this autobox process. So a new Short object is created even if you don't do

As c1 and cb within go() is eligible for GC. so count is 2

The go() method is invoked on the object c1 is referencing, but c1 is not changed in this method so the corresponding object will not be eligible for GC.

John


so if the question is modified as the 200 become 100, what will be the possible answer?
 
John Stark
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case I would say only one object is eligible for GC.

John
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why C1 is not eligible for GC who else is pointing to object created in line 1.




John Stark wrote:

The go() method is invoked on the object c1 is referencing, but c1 is not changed in this method so the corresponding object will not be eligible for GC.

John


 
John Stark
Ranch Hand
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, sorry I was only looking at
John
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic