Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Static Variables - Garbage Collection Process

 
Ranch Hand
Posts: 431
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF we have some static variables in the class, then, at what time they are garbage collected?
 
Ranch Hand
Posts: 42
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:IF we have some static variables in the class, then, at what time they are garbage collected?




The static variable will live as long as the class is loaded in the JVM. When there are no more instances of the class being ran in the JVM the class will be unloaded and the static variable will be eligable for garbage collection.
 
Bartender
Posts: 7208
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"The only way that a Class can be unloaded is if the Classloader used is garbage collected. This means, references to every single class and to the classloader itself need to go the way of the dodo."

If you think about it, classes cannot be garbage collected just because there are no instances of the class. Some classes only have static variables and methods so no instance can ever be created. Classes are loaded when they are referenced for the first time and only unloaded if the classloader is garbage collected.

To answer the original question: for all practical purposes the answer is that they are never garbage collected.
 
Bartender
Posts: 4007
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind the difference between a reference variable and the object it references. Suppose we have

what will be garbage collected (if at all)?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Keep in mind the difference between a reference variable and the object it references. Suppose we have

what will be garbage collected (if at all)?



So once you say p = null, the Point object that was initially created on the heap memory becomes an orphan. So it becomes available for garbage collection.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic