1. 程式人生 > >深入理解Java虛擬機器——Java堆測試

深入理解Java虛擬機器——Java堆測試

  • 程式碼Test案例是對Java堆,新生代、老年代的理解和認識,對GC回收機制的應用
  • 1.直接執行只展示Java堆記憶體的使用情況。
  • 2.在執行之前,eclipse或Idea配置JVM執行引數(見程式碼註釋中),執行可得詳細資訊。

知識點都在註釋裡

package demo;

public class TestJvm {

    public static void main(String[] args) {

        long maxMemory = Runtime.getRuntime().maxMemory();
        long freeMemory = Runtime.getRuntime
().freeMemory(); long totalMemory = Runtime.getRuntime().totalMemory(); //-Xms5m -Xmx20m -XX:+PrintGCDetails -XX:+UseSerialGC -XX:+PrintCommandLineFlags //-Xms5m 程式初始化分配的堆記憶體大小是5m //-Xmx20m 程式能獲得分配的最大記憶體是20M //-XX:+PrintGCDetails 控制檯列印GC的詳細日誌 -XX:+PrintGC 控制檯列印簡單日誌 //-XX:+UseSerialGC 配置序列回收器 //-XX:+PrintCommandLineFlags 列印傳給虛擬機器的配置引數 /*eg: 執行Demo,給定虛擬機器引數如下 -XX:InitialHeapSize=5242880 -XX:MaxHeapSize=20971520 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC */
System.out.println("最大分配的記憶體空間:"+maxMemory); System.out.println("空閒的記憶體空間:"+freeMemory); System.out.println("一共分配的記憶體空間:"+totalMemory); byte[] b1 = new byte[1*1024*1024]; maxMemory = Runtime.getRuntime().maxMemory(); freeMemory = Runtime.getRuntime().freeMemory
(); totalMemory = Runtime.getRuntime().totalMemory(); System.out.println("分配了1M記憶體"); System.out.println("最大分配的記憶體空間:"+maxMemory); System.out.println("空閒的記憶體空間:"+freeMemory); System.out.println("一共分配的記憶體空間:"+totalMemory); byte[] b2 = new byte[5*1024*1024]; maxMemory = Runtime.getRuntime().maxMemory(); freeMemory = Runtime.getRuntime().freeMemory(); totalMemory = Runtime.getRuntime().totalMemory(); System.out.println("分配了5M記憶體"); System.out.println("最大分配的記憶體空間:"+maxMemory); System.out.println("空閒的記憶體空間:"+freeMemory); System.out.println("一共分配的記憶體空間:"+totalMemory); int b = 0x00000000fee10000; int a = 0x00000000fec00000; System.out.println("新生代記憶體空間大小為: "+ (b-a)/1024); } }

執行結果

-XX:InitialHeapSize=5242880 -XX:MaxHeapSize=20971520 -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseSerialGC 
[GC (Allocation Failure) [DefNew: 1664K->191K(1856K), 0.0012625 secs] 1664K->601K(5952K), 0.0082026 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
最大分配的記憶體空間:20316160
空閒的記憶體空間:5402192
一共分配的記憶體空間:6094848
分配了1M記憶體
最大分配的記憶體空間:20316160
空閒的記憶體空間:4334600
一共分配的記憶體空間:6094848
[GC (Allocation Failure) [DefNew: 1339K->11K(1856K), 0.0058954 secs][Tenured: 1625K->1636K(4096K), 0.0014178 secs] 1749K->1636K(5952K), [Metaspace: 3266K->3266K(1056768K)], 0.0074605 secs] [Times: user=0.00 sys=0.02, real=0.01 secs] 
分配了5M記憶體
最大分配的記憶體空間:20316160
空閒的記憶體空間:4450208
一共分配的記憶體空間:11407360
新生代記憶體空間大小為: 2112
Heap
 def new generation   total 1920K, used 86K [0x00000000fec00000, 0x00000000fee10000, 0x00000000ff2a0000)
  eden space 1728K,   5% used [0x00000000fec00000, 0x00000000fec15b48, 0x00000000fedb0000)
  from space 192K,   0% used [0x00000000fedb0000, 0x00000000fedb0000, 0x00000000fede0000)
  to   space 192K,   0% used [0x00000000fede0000, 0x00000000fede0000, 0x00000000fee10000)
 tenured generation   total 9220K, used 6756K [0x00000000ff2a0000, 0x00000000ffba1000, 0x0000000100000000)
   the space 9220K,  73% used [0x00000000ff2a0000, 0x00000000ff9393c8, 0x00000000ff939400, 0x00000000ffba1000)
 Metaspace       used 3287K, capacity 4500K, committed 4864K, reserved 1056768K
  class space    used 359K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 0