• 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

Static reference and GC

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do objects assigned to a static reference ever get garbage collected in java.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ohimai anthony wrote:do objects assigned to a static reference ever get garbage collected in java.


It's irrelevant whether a reference is static or not. Any object that has a reachable variable (static or otherwise) referring to it cannot be garbage collected.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However it is true that the containers for static variables (i.e. classes) are much less likely to be garbage-collected than the containers for instance variables (i.e. objects). So it may appear that objects referred to by static variables cannot be garbage-collected. That's not the case, though; in complex environments with multiple class loaders it is possible for classes to be garbage-collected.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So it may appear that objects referred to by static variables cannot be garbage-collected



So how does one say "I dont want to use that class anymore, you can garbage collect it"?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar M K wrote:So how does one say "I dont want to use that class anymore, you can garbage collect it"?



A class can be garbage-collected only after the class loader which loaded it is garbage-collected. So this means that first of all you have to create your own class loader; the standard system class loaders are never collected. Then you have it load some classes. Finally you arrange things so that neither those classes nor your class loader have any references, and they can then be collected.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right..thanks!
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you mean "I don't want to use that object anymore" or did you mean "I don't want to use that class anymore". There's a big differrence. An object is eligible for GC if there are no references to it. So, if a static variable has a reference to an object and you make that variable null, and there are no other references to the same object, then that object will get GCed.

Unloading of classes is a totally differrent matter. Most of the times you on't really need to GC classes. Unless you have a custom classloader, classes will be loaded only once.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant Class objects...Am thinking that Class objects come under the purview of system class loaders...
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jut fyi, we're straying beyond what's on the exam...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic