• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object References & GC

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh thanks for that information Bert
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Aditya
 
Ranch Hand
Posts: 114
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic