• 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

GC

 
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
code snippet:
-------------
ClassA{
private static ClassB obj= null;
public static void init() {
obj = new ClassB();
obj = new ClassB();
}
}
public Class Test{
public static void main(String[] args) {
ClassA.init();
/* some process */

}
}
In ClassA, two objects of ClassB are created. I would like to know when these objects are eligible for GC. Is it only when ClassA is unloaded??
Thanx,
shankar.
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first ClassB instance is eligible for garbage collection when the obj variable is reassigned to the second instance. The second instance will be eligible when obj is again reassigned or when ClassA is unloaded.
Matthew Phillips
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello both
Because the second instance is a local variable, and it is not reasigned before the end of the method, I thinkthe object that points will be made eligable for g.c. when the method in which is declared returns.
 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: "obj" is static. does this make any difference???
reply
    Bookmark Topic Watch Topic
  • New Topic