• 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: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is a variable eligible for garbage collection when it goes out of scope?
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garabage collcetion is applied only for objects and not for variables.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vicken Karaoghlanian:
Garabage collcetion is applied only for objects and not for variables.



Quite correct. The reason is that (local) variables life on the stack, not on the heap. The stack memory is automatically released when the method is finished.
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a local variable refers to an object and the local variable goes out of scope and there are no other references to that object then the object becomes eligible for garbage collection. I suspect that might be what you were getting at with your question but then again I may just be way off the mark...

Jules
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Julian Kennedy:
If a local variable refers to an object and the local variable goes out of scope and there are no other references to that object then the object becomes eligible for garbage collection.



I am not sure that this is true. At the byte code level, all local variables have method scope, so I think that it isn't guaranteed that a reference to an object from a local variable gets lost before the containing method is finished.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
I am not sure that this is true. At the byte code level, all local variables have method scope, so I think that it isn't guaranteed that a reference to an object from a local variable gets lost before the containing method is finished.

It isn't guaranteed, and in older JVMs it doesn't happen. The Sun 1.4 JVM can garbage collect a variable as soon as it goes out of scope, though, even before the method terminates and the stack frame gets cleaned up.

- Peter
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a feeling that was going to get stomped on when I wrote it. Thanks Ilja and Peter for the extra detail; it's always good to know.

Jules
 
Anamika Agarwal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for clearing my doubt
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic