• 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 of static variables

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does garbage collection work on static variables? I think since the static variables don't have any references pointing to them , but always lives in memory once the class is loaded, garbage collection is not possible on static variables. Is that true?
Any thoughts on this?

Thanks!
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that variables in general are normally allocated on the stack, not the heap, so they are not available for garbage collection. (When they go out of scope, they simply go away, like when a method returns). Now for the objects they point to- they are on the heap and subject to garbage collection.

So the static variables themselves, I don't know where they are, but I would suspect they are on the stack, in the "main stack frame" area which only goes out of scope when the program terminates, but I don't know this for a fact I am guessing.

Now the objects that a static reference points to could be in the heap, and available for garbage collection when that reference points to something new.
 
Nina Binde
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tom. That helps. Just to reiterate what you mentioned, the objects that the static references point to can be garbage collected , but the static references themselves do not get garbage collected until after the program terminates.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So the static variables themselves, I don't know where they are, but I would suspect they are on the stack, in the "main stack frame" area which only goes out of scope when the program terminates, but I don't know this for a fact I am guessing

I believe that local variables are on the stack but static and instance variables are parts of objects kept in the heap.
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic