• 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

Garbage Collection

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ranchers!
I have some doubt about garbage collections. Does JVM garbage collect the objects instantiated only with keyword new? If I have the code like the below, how many objects will be eligible for gc?



Also, can someone please explain me what kind of back-end work compiler does when I do the following?


Does aInt become a variable in the heap somehow because compiler changes to Integer aInt = new Integer(5); on the backend? whereas



will create a variable on the stack? Is my intuition correct?

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jae,

What is taking place here:

Integer aInt = 5;

is a boxing conversion (the int expression on the right hand of the assignment will be boxed into an Integer object that contains the value 5.) Also, according to the JLS, 5.1.7 (Boxing Conversion):


If the value p being boxed is true, false, a byte, a char in the range \u0000 to
\u007f, or an int or short number between -128 and 127, then let r1 and r2 be
the results of any two boxing conversions of p. It is always the case that r1 ==
r2.



What this means is that every proper java implementation is required to have a pool of Integer (and other type of wrapper types, as specified in the JLS fragment I provided) objects similar to the String Constant Pool. In this case, since 5 is in that range, what will happen is that a new Integer object representing a value of 5 will be created in this special pool, so even if you null your reference, I believe the Integer object itself won't be garbage collected (but this is not entirely clear.) I need to know how the garbage collector interacts with that special wrapper pool (does it actually clear an object in that pool once it verifies that there are no references pointing to it, or does it leave it there?) Someone else might answer that, but from what the JLS says (it mentions that each implementation is left to implement a "lazy or eager" caching policy) it appears this might be left as an implementation detail.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anyone knows the answer?

how many objects will be eligible for gc in this case?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rod,

Welcome to Javaranch!

I am not sure about the answer because of the pool issue. At least 4 objects (the 2 A objects, plus the 2 Double objects which aren't cached) and at most 5 objects (including the Integer, which is shared between both A instances due to the fact that it is cached in the pool.) Perhaps someone has a more authoritative answer, although I have the feeling this would be implementation dependent, and you wouldn't find this question in the exam. But I would like to hear the official word on that too.
 
Jae Lim
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Hi Rod,

Welcome to Javaranch!


I am not sure about the answer because of the pool issue. At least 2 objects (the 2 A objects,) and at most 4 objects (including the Integer and Double, which are shared between both A instances due to the fact that they are pool objects.) Perhaps someone has a more authoritative answer, although I have the feeling this would be implementation dependent, and you wouldn't find this question in the exam. But I would like to hear the official word on that too.



Hello Rod. I took my first SCJP6 exam about 2 weeks ago and this type of question was actually on the exam. That was the reason I posted this question. Thanks for the opinions everyone. Now based on the understanding that the wrapper objects get created in the pool, is it safe to assume that gc doesn't run on those pools because as far as I'm concerned, gc only runs on memory (heap)
 
Rod Oliver
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is 5 ... because the object "aInt" is created only once, because its value is less than 127, then the two references point to the same.

Objects collecteds by GC:
2 objects of class A.
1 new Integer("5").
2 new Double("5.0").


...but, still waiting confirmation.

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jae Lim wrote:

Ruben Soto wrote:Hi Rod,

Welcome to Javaranch!


I am not sure about the answer because of the pool issue. At least 2 objects (the 2 A objects,) and at most 4 objects (including the Integer and Double, which are shared between both A instances due to the fact that they are pool objects.) Perhaps someone has a more authoritative answer, although I have the feeling this would be implementation dependent, and you wouldn't find this question in the exam. But I would like to hear the official word on that too.



Hello Rod. I took my first SCJP6 exam about 2 weeks ago and this type of question was actually on the exam. That was the reason I posted this question. Thanks for the opinions everyone. Now based on the understanding that the wrapper objects get created in the pool, is it safe to assume that gc doesn't run on those pools because as far as I'm concerned, gc only runs on memory (heap)


I am a little surprised that a question like this would be on the exam. I also think that the GC probably doesn't touch the cached wrapper objects, but I haven't specifically read that anywhere, and that's why I was guessing it was an implementation detail. Do you know where that is specified?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rod Oliver wrote:I think the answer is 5 ... because the object "aInt" is created only once, because its value is less than 127, then the two references point to the same.

Objects collecteds by GC:
2 objects of class A.
1 new Integer("5").
2 new Double("5.0").


...but, still waiting confirmation.


You are right, Doubles are not supposed to be cached. But the Integer might not be garbage collected because it's in the pool. I would also like to know if it can be guaranteed that any garbage collection implementation won't touch these objects.
 
Would you like to try a free sample? Today we are featuring tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic