aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Garbage Collection" Watch "Regarding Garbage Collection" New topic
Author

Regarding Garbage Collection

KrishnaPrasad raghavan
Ranch Hand

Joined: Oct 28, 2008
Posts: 46
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

Joined: Feb 24, 2006
Posts: 6662
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Cindy Carney
Ranch Hand

Joined: Jan 12, 2010
Posts: 57
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

Joined: Nov 04, 2009
Posts: 2066

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!



|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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

Joined: Nov 04, 2009
Posts: 2066


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

Joined: Feb 24, 2006
Posts: 6662
    
    5

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

Joined: Nov 04, 2009
Posts: 2066

Ok. Is it only enough?!

Thanks Guys....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Abimaran Kugathasan wrote:Ok. Is it only enough?!

What is enough??
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Garbage Collection