深入理解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
    原文作者:java虚拟机
    原文地址: https://blog.csdn.net/qq_37192800/article/details/80891355
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞