• 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 field

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
when you first time access the static object in a class. The class is loaded and static object is initialized. It will not be reinitialized.
If you don't use it again, does the garbage collector will release the memory like non static object? or does it always stay in the memory until the program finishs? If it is running on a server, does it always in the memory and ready to be used until you stop the server?
Thanks
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I jumped in the middle of a big discussion about this and it was generally the consensus that trying to use the gc results in problematic issues that really only an experienced engineer can approach effectively and reliably. If your code just left a large chunk of work and you know that the program will probably not be busy for a awhile, then you can put a call to gc there if you want to but I do not use it at all after that discussion.

Static anything is probably not something I would think the gc would be applied to as much as anything you called by using the new operator, and if such an object, created by new, either goes out of scope leaves { }or is set to null ojectOne = null;// not needed any more. then the static becomes eligible for gc like anything else that no longer is reachable by the program without calling new again.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you don't use it again, does the garbage collector will release the memory like non static object? or does it always stay in the memory until the program finishs? If it is running on a server, does it always in the memory and ready to be used until you stop the server?



Whether something is garbage collected is dependent on whether it is reachable -- period. It doesn't matter if it is reachable from static variables of classes, stacks frames from threads of execution, or objects which are reachable. If it is reachable, it will not be garbage collected.

Henry
 
dennis liang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,
I know when non static objects are not reachable, but I don't know when static objects are not reachable? I thought they were always there until the program ended.
Thanks
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dennis liang:
Henry,
I know when non static objects are not reachable, but I don't know when static objects are not reachable? I thought they were always there until the program ended.
Thanks



There is no distinction of "non static objects" and "static objects" -- an object is an object. Just because it is referenced by a static variable doesn't change it behavior. When it is no longer reachable, via from both static and non static references, it will be garbage collected.

Henry
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aside from the fact that the compiler will probably catch this, this is what the typical mistake looks like in general form:



These are hard to catch in a large code project.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects referenced by static variables (only) will remain reachable until the class itself is unloaded. In current JVMs a class won't be unloaded until the ClassLoader that loaded the class is GCd. Only classloaders created by the application (for example, custom classloaders are common in application servers) will ever be GCd, and then only if the application itself lets go of them.

So for many applications, the answer is that object referenced by static variables will never be collected. But in something like a Servlet container, when a servlet is reloaded, its classes will be discarded and therefore those objects could be reclaimed.
 
dennis liang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//first file
public class PoolClass {

private static PoolClass pool = new PoolClass();

public static PoolClass getInstance(){
return pool;
}

private PoolClass() {
//...
}

}

//second file
public class Client{
public static void main(String[] args){
...
...
...
PoolClass poolOne = PoolClass.getInstance();
...
...
...
//could be short or long
...
...
PoolClass poolTwo = PoolClass.getInstance();
}
}

are poolOne and poolTwo the same object or different objects?

Thanks
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


EFH is more accomplished at this OO stuff so let me let him answer questions you may have about the code I posted here - a rework of your code.


are poolOne and poolTwo the same object or different objects?
Yes.
[ January 18, 2008: Message edited by: Nicholas Jordan ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic