静态变量是在程序执行的整个持续时间内分配的,因此堆栈和堆都不方便.那它在哪里?应该有一些装载的地方?
这个问题特别针对java语言.我知道为所有静态代码留出了一个静态的内存.
我的问题是这个静态内存是如何填充的?静态对象是在导入时还是在第一次引用时放入静态内存?此外,相同的垃圾收集规则是否适用于静态对象,因为它们适用于所有其他对象?
public class Example{
public static SomeObject someO = new SomeObject();
}
/********************************/
// Is the static object put into static memory at this point?
import somepackage.Example;
public class MainApp{
public static void main( Sting args[] ){
// Or is the static object put into memory at first reference?
Example.someO.someMethod();
// Do the same garbage collection rules apply to a
// static object as they do all others?
Example.someO = null;
System.gc();
}
}
Run Code Online (Sandbox Code Playgroud)