• 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

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

If a class A has a static reference to an object B, Will object B be garbage-collected? When?
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops, misread

Whether a reference is static or not doesn't matter.
Your B has a reference pointing to it so its reference count is larger than 0.
Therefore it won't be elligible for garbage collection.

As the reference is static is will never be garbage collected until the JVM goes down itself and cleans up the class object for A, unless some mechanism sets the reference to it to null.
[ June 02, 2004: Message edited by: Jeroen Wenting ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, The reference of the Object B (Static member)can be garbage collected.

When :- Till the class is loaded Static members will be in memory.
once the class is about to be unloaded Static Members will be garbage collected.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the question is, does the class ever get garbage collected?

The Java Language Specification, Sun set out the rules of engagement for class unloading. Classes loaded by the bootstrap classloader (Java core classes) never get unloaded. Other classes can unload if their classloader becomes unreachable. In practice, this will happen for instance in J2EE application servers with hot deploy.

- Peter
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, that's what I thought initially

The question is if an object to which a static reference exists can at some point be garbage collected.
The answer to that is a resounding "yes", namely the moment that reference is set to null
 
Peter den Haan
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 Jeroen Wenting:
The answer to that is a resounding "yes", namely the moment that reference is set to null

The moment you set the reference to null, the object ceases to be "an object to which a static reference exists".

Objects to which a static reference exists get garbage collected when their class gets garbage collected, which should happen only when their classloader becomes unreachable and available for garbage collection.

- Peter
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic