• 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

Objects available for gc?

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading through one of Corey's blogs on garbage collection and have a question. I have posted the accompanying code below:

After methodA executes, what is available for garbage collection? I would expect the answer to be two (d and e) for the following reasons:

1. c is never initialized and hence no object is created to be available for garbage collection
2. the object created/assigned to b is now referred to by the instance variable a
3. d is a local variable that has its scope limited to methodA
4. e is a local variable that has its scope limited to methodA

Can someone please confirm the above expectations?
[ July 09, 2004: Message edited by: Chris Allen ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, there are no objects eligible for garbage collection when methodA completes. Here's the explanation...



The array that is created (and referenced by b) is returned to the invoking method and is later referenced by the instance variable, a. This array, therefore, can not be garbage collected.

The variable c is never initialized. This variable, therefore, refers to null and there is no object to collect.

The variable d refers to a boolean primitive. Primitives are not objects and are not subject to garbage collection. Primitives can be ignored for the purposes of garbage collection.

The variable e refers to a String literal. String literals are never garbage collected (at least not as most objects are). This is because String literals are referenced from the constant String Literal Pool. Don't worry about this - it's not on the exam. If you're interested, you can search this forum and find a lot of info on it, though.

That's it. Every object is accounted for and nothing is eligible for garbage collection.
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Corey. Just to complete my understanding, I have changed the methodA definition slightly as below:

I have added the wrapper class Integer variable definition and and Object definition. Would f and g be eligible for garbage collection at the end of the method? I think this covers off every type of variable that could be defined (i.e. primitive, wrapper, array, reference variable) to determine the gc rules. Thanks for clarifying that primitives are not eligible for the gc rules.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, this won't even compile:



An Integer (the wrapper object, not the primitive, int) is an object. You can't assign a primitive value to an object reference variable. If you wanted to make an Integer object with the value of 5, you should do this:



Given that you've done that correctly, both of the objects originally referred to by f and g would be eligible for garbage collection when methodA terminates. The reason for this is that the only references to those objects, f and g, will have gone out of scope. Once those references are gone, the objects will have no active references and will be eligible for GC.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi Corey,

If methodA does not return b and is a void method then what will be the state of variables a and b???

will they be garbage collected after the termination of methodA??

Kaps
[ July 09, 2004: Message edited by: kapil munjal ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if methodA is a void method, you can't have this line:



This won't work because methodA doesn't return anything. Therefore, the variable a will reference nothing and there's no need to collect anything.

As far as the array originally referenced by b goes, it would be just the same as the other local objects in methodA. Once methodA terminates, that object will be eligible for garbage collection because there would then be no active references to it.

Be sure to give this a read.
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:
[QB]First of all, this won't even compile:



An Integer (the wrapper object, not the primitive, int) is an object. You can't assign a primitive value to an object reference variable. If you wanted to make an Integer object with the value of 5, you should do this:




Sorry about that. I should have compiled in advance of the posting. Thanks for responding in spite of the 'newbie' error.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic