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
Nicholas Jordan
Ranch Hand
Joined: Sep 17, 2006
Posts: 1282
posted
0
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.
"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
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, 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
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
Joined: Sep 17, 2006
Posts: 1282
posted
0
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.
Ernest Friedman-Hill
author and iconoclast
Marshal
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.
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
Joined: Sep 17, 2006
Posts: 1282
posted
0
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 ]
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.