java 静态(static)变量放在那里?

    java的静态变量我一直纠结放在堆中,还是永久代中,今天来做个试验:

import java.util.HashMap;

public class Test {
    static HashMap ha = new HashMap();
    public static void main(String args[]) {
      for(int i=0; i<10000000; i++){
          ha.put(i, "aa"+i);
      }
    }
}
-Xms10m -Xmx10m -XX:PermSize=5m -XX:MaxPermSize=5m   -XX:+HeapDumpOnOutOfMemoryError
java.lang.OutOfMemoryError: GC overhead limit exceeded
Dumping heap to java_pid4256.hprof ...
Heap dump file created [15474096 bytes in 0.190 secs]
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.HashMap.createEntry(HashMap.java:897)
at java.util.HashMap.addEntry(HashMap.java:884)
at java.util.HashMap.put(HashMap.java:505)
at Test.main(Test.java:9)


-Xms50m -Xmx50m -XX:PermSize=5m -XX:MaxPermSize=5m   -XX:+HeapDumpOnOutOfMemoryError
java.lang.OutOfMemoryError: GC overhead limit exceeded
Dumping heap to java_pid8992.hprof ...
Heap dump file created [74370437 bytes in 0.583 secs]
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.HashMap.createEntry(HashMap.java:897)
at java.util.HashMap.addEntry(HashMap.java:884)
at java.util.HashMap.put(HashMap.java:505)
at Test.main(Test.java:9)

   从试验的结果来看,第一次堆设置是10M,产生内存溢出的文件是15M;第二次堆设置为50M,产生内存溢出的文件是75M。分析dump文件可以看到第一次HashMap占用9M,第二次HashMap占用48M。而永久代设置的是5M,显然静态变量在堆内存中

你可能感兴趣的:(java基础)