• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Static reference

 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
 
I am a man of mystery. Mostly because of this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic