这个问题针对的是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();
}
}
static
字段,可以解释为“实际上不是卸载,而是丢弃可重建数据”。在这方面,今天已经发生了一些事情,例如当使用“共享类数据”存档时,它被内存映射到进程中,因此,其中的一部分可能会从物理内存中透明地删除并由操作系统重新加载。此外,很少使用的代码可能会被去优化,丢弃编译代码。足够接近了... - Holger