怎么了java中的静态内存? [英] Whats up with static memory in java?

查看:82
本文介绍了怎么了java中的静态内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题特别针对java语言。我知道为所有静态代码留出了静态内存。



我的问题是这个静态内存是如何填充的?静态对象是在导入时还是在第一次引用时放入静态内存?此外,相同的垃圾收集规则是否适用于静态对象,因为它们适用于所有其他对象?

  
公共类示例{
public static SomeObject someO = new SomeObject();
}
/ ******************************** /
//是此时静态对象放入静态内存?
import somepackage.Example;

公共类MainApp {
public static void main(Sting args []){
//或者是第一次引用时放入内存的静态对象?
Example.someO.someMethod();
//同样的垃圾收集规则是否适用于
//静态对象,因为它们执行所有其他操作?
Example.someO = null;
System.gc();
}
}


解决方案

进口不与编译代码中的任何指令相关联。它们只在编译时建立别名。



有一些反射方法允许加载类但尚未初始化,但在大多数情况下,你可以假设每当引用一个类时,它都已初始化。



执行静态成员初始值设定项和静态块,就好像它们都是源代码顺序中的一个静态初始化程序块一样。 / p>

在卸载类之前,强引用通过静态成员变量引用的对象。普通的 ClassLoader 从不卸载类,但应用程序服务器使用的类在正确的条件下执行。然而,它是一个棘手的领域,并且是许多难以诊断的内存泄漏的来源 - 而且是另一个不使用全局变量的原因。






作为(切向)奖金,这是一个需要考虑的棘手问题:

  public class Foo {
private static Foo instance = new Foo();
private static final int DELTA = 6;
private static int BASE = 7;
private int x;
private Foo(){
x = BASE + DELTA;
}
public static void main(String ... argv){
System.out.println(Foo.instance.x);
}
}

这段代码会打印什么?尝试一下,你会看到它打印6。这里有一些工作,一个是静态初始化的顺序。代码执行就好像它是这样编写的:

  public class Foo {
private static Foo instance;
private static final int DELTA = 6;
private static int BASE;
static {
instance = null;
BASE = 0;
instance = new Foo(); / *在计算instance.x时,BASE为0。 * /
BASE = 7;
}
private int x;
private Foo(){
x = BASE + 6; / *6是内联的,因为它是一个常量。 * /
}
}


This question is for the java language in particular. I understand that there is a static protion of memory set aside for all static code.

My question is how is this static memory filled? Is a static object put into static memory at import, or at first reference? Also, do the same garbage collection rules apply to static objects as they do for all other objects?


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();
    }
}

解决方案

Imports don't correlate with any instructions in compiled code. They establish aliases for use at compile time only.

There are some reflective methods that allow the class to be loaded but not yet initialized, but in most cases, you can assume that whenever a class is referenced, it has been initialized.

Static member initializers and static blocks are executed as if they were all one static initializer block in source code order.

An object referenced through a static member variable is strongly referenced until the class is unloaded. A normal ClassLoader never unloads a class, but those used by application servers do under the right conditions. However, it's a tricky area and has been the source of many hard-to-diagnose memory leaks—yet another reason not to use global variables.


As a (tangential) bonus, here's a tricky question to consider:

public class Foo {
  private static Foo instance = new Foo();
  private static final int DELTA = 6;
  private static int BASE = 7;
  private int x;
  private Foo() {
    x = BASE + DELTA;
  }
  public static void main(String... argv) {
    System.out.println(Foo.instance.x);
  }
}

What will this code print? Try it, and you'll see that it prints "6". There are a few things at work here, and one is the order of static initialization. The code is executed as if it were written like this:

public class Foo {
  private static Foo instance;
  private static final int DELTA = 6;
  private static int BASE;
  static {
    instance = null;
    BASE = 0;
    instance = new Foo(); /* BASE is 0 when instance.x is computed. */
    BASE = 7;
  }
  private int x;
  private Foo() {
    x = BASE + 6; /* "6" is inlined, because it's a constant. */
  }
}

这篇关于怎么了java中的静态内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆