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

Object References & GC

 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All

Integer i=100;

Is this Integer reference 'i' created in the heap or in the stack and are they eligible for GC.
I know Integer i=new Integer(100); will create an object in the heap and they are eligible for GC.

Thanks
Srividhya
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srividhya!

The line of code

is almost equivalent to

This means that, in a similar manner, the 'object' is created on heap and the reference 'i' is created on stack.

However, there is a fine line of difference.

For facilitating autoboxing in an efficient manner, JVM keeps some objects of wrapper classes in cache... how many of which class can be found in some book, but as far as I can recollect, its -128 to +127 for all numeric (non-floating types) wrapper classes and 0 to 255 for Character. Please refer to some book on this.

All this means that there won't be a new object getting 'created' by

if there exists an Integer object in cache representing value 100. You could say

will print 'true'. Whereas,

will print 'false'.

Moreover, if the value is greater than +128 (or whatever the limit is), then

will print 'false'. There will be 2 distinct objects created and they won't be cached by JVM.


Is this Integer reference 'i' created in the heap or in the stack and are they eligible for GC.
I know Integer i=new Integer(100); will create an object in the heap and they are eligible for GC.


As far as availability for GC is concerned, objects which are cached by JVM will not be eligible for GC. This would mean that

will not make any object available for GC at all. Whereas

will make the newly created Integer object available for GC.

Hope I make myself clear.

Best regards,

- Aditya Jha
[ May 14, 2008: Message edited by: Aditya Jha ]
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aditya

Thanks for your detailed reply. I have some more doubt to be clarified.


1. Here s.i and s1.i refer to same Integer reference 100 in the cache.
2. Suppose if I make s=null, only one object will be eligible for GC which is first GC object 's' and not 'i' since it is referenced by s1.i.
3. If I change the value of i from 100 to Integer i=128 then two objects are eligible for GC which is 's' and the Integer reference s.i. Since s.i and s1.i refer to two different Integer references.

Correct me if I am wrong.

Srividhya
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an interesting and tricky topic. The good news is that, for the SCJP exam, you won't have to deal with how the GC interacts with objects that can go into pools or caches, etc. So, GC questions won't use wrapper type objects or Strings.
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh thanks for that information Bert
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, here are your answers:


1. Here s.i and s1.i refer to same Integer reference 100 in the cache.

True.


2. Suppose if I make s=null, only one object will be eligible for GC which is first GC object 's' and not 'i' since it is referenced by s1.i.

You are correct. Also, I would like to add that even if you nullify 's' as well as 's1', the only objects available for garbage-collection would be the two objects of class GC. The object of class Integer will be referred by internal cache of JVM, and will never be available for gc.


3. If I change the value of i from 100 to Integer i=128 then two objects are eligible for GC which is 's' and the Integer reference s.i. Since s.i and s1.i refer to two different Integer references.

That is correct!

Best regards,

- Aditya Jha
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Aditya
 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want some more Information about Pool and caches for the purpose of knowing it in detailed because GC is my most favorite topic, I never miss it. I want to know How and When wrapper classes objects and String objects are eligible for GC. I want to know with a very fine Information about GC which i can remember throughout my lifetime and only you java ranch people can help me and others.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic