Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Garbage Collection

 
KrishnaPrasad raghavan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have this doubt regarding Garbage Collection.

Lets say we have class called Sample.



Lets Imagine the reference pointing to becomes null


I understand 2 objects will be eligible for garbage collection. One is the Class Sample and one is the Wrapper class within the Class Sample. int "a" will not be eligible right, since its a primitive type. Can someone please confirm if my understanding is correct.

Source of this question : I created this question myself. Apologies if this question has been asked before..
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly, Integer x=20 will create the integer in the pool ? Thus the number of objects to be collected is 1.

If the number were something like 234453 instead of 20, the answer should be 2.
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak is right, since the value 20 is in the range of -128 to 127, so the Integer object created by autoboxing will be from the integer constant pool hence only one object will be eligible for GC...
 
Cindy Carney
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to say '2 instances' somehow, instead of 'Class Sample.' As I understand it, classes themselves never become eligible for garbage collection - just instances of those classes.

Also - if you want to get around the '-128 to 128 Wrapper classes are reused' issue, change your "Integer x = 20" to "Integer x = new Integer(20)" instead. This guarantees you create a new object, instead of reusing low-valued wrapper objects.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Deepak is right, since the value 20 is in the range of -128 to 127, so the Integer object created by autoboxing will be from the integer constant pool hence only one object will be eligible for GC...
.

Even though, after the object s is destroyed, is there a reference to that wrapper Integer object? Or

Integer constant pool object are not eligible for GC, because it can be reused by other object(It's the intention of the integer constant pool)?

And, is it same for String literal pool(even though it's not in the exam)?. Thanks in Advanced!


 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:Integer constant pool object are not eligible for GC, because it can be reused by other object(It's the intention of the integer constant pool)?

Yes, this is right...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And, is it same for String literal pool(even though it's not in the exam)?. Thanks in Advanced!


Thanks Ankit Garg, then this one also isn't correct?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, is it same for String literal pool(even though it's not in the exam)?. Thanks in Advanced!


The exam does expect you to understand what the String pool is
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Is it only enough?!

Thanks Guys....
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:Ok. Is it only enough?!

What is enough??
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Bala wrote:
And, is it same for String literal pool(even though it's not in the exam)?. Thanks in Advanced!


The exam does expect you to understand what the String pool is


Only understand about String Constant Pool is enough? I really don't know, what types of questions will be there with related to String Constant Pool?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic