zoukankan      html  css  js  c++  java
  • JVM 堆参数调优 (四)

     堆参数调优

    1、堆的结构

     JAVA7 

      堆逻辑上分为:新生区、养老区、永久区;实际上堆只有新生区、养老区

       Minor GC:轻量的垃圾回收;   Major GC(Full GC):重量级垃圾回收。

      

    Java8 

      没有永久区了,被元空间取代;

      

     2、堆内存调优

      -Xms:设置初始分配大小,默认为物理内存的 “ 1 / 64”;

      -Xmx:最大分配内存,默认为物理内存的 “1 / 4”;

      -XX:+PrintGCDetails:输出详细的GC处理日志;

     (1)使用代码输出实际默认的内存,代码如下:

    public class Demo1
    {
        public static void main(String[] args)
        {
            long totalMemory = Runtime.getRuntime().totalMemory(); //JVM中的初始内存总量
            long maxMemory = Runtime.getRuntime().maxMemory(); //JVM试图使用的最大内存
    
            System.out.println("totalMemory = " + totalMemory + "Byte 、 " +
                    (totalMemory / (double) 1024 / 1024) + " MB");
            System.out.println("MaxMemory = " + maxMemory + " Byte 、 " +
                    (maxMemory / (double) 1024 / 1024) + " MB");
        }
    }

      结果如下:(物理内存:12G

      totalMemory = 191365120Byte 、 182.5 MB        // 182.5 * 64 / 1024

      MaxMemory = 2831679488 Byte 、 2700.5 MB   // 2700.5 * 4 / 1024  

         结论:发现默认的情况下分配的内存是总内存的 “1 / 4”、而初始化内存为 “1 / 64”。

    (2)在IDEA中设置VM参数:

      VM 参数: -Xms1024M -Xmx1024M -XX:+PrintGCDetails

     运行结果: 

      totalMemory = 1029177344Byte 、 981.5 MB   //1024M

      MaxMemory = 1029177344 Byte 、 981.5 MB

      Heap

      PSYoungGen      total 305664K, used 20971K [0x00000000eab00000, 0x0000000100000000, 0x0000000100000000)

        eden space 262144K, 8% used [0x00000000eab00000,0x00000000ebf7afb8,0x00000000fab00000)

        from space 43520K, 0% used [0x00000000fd580000,0x00000000fd580000,0x0000000100000000)

        to   space 43520K, 0% used [0x00000000fab00000,0x00000000fab00000,0x00000000fd580000)

       ParOldGen       total 699392K, used 0K [0x00000000c0000000, 0x00000000eab00000, 0x00000000eab00000)

        object space 699392K, 0% used [0x00000000c0000000,0x00000000c0000000,0x00000000eab00000)

       Metaspace       used 3516K, capacity 4500K, committed 4864K, reserved 1056768K

        class space    used 389K, capacity 392K, committed 512K, reserved 1048576K

            计算堆内存: (305664 + 699392)/ 1024 = 981.5 MB

     3、MMO异常的代码:

    为了更快的产生OOM,设置如下参数; 

            VM 参数: -Xms8M -Xmx8M -XX:+PrintGCDetails 

    public class Demo2
    {
        public static void main(String[] args)
        {
            String str = "hello world";
            while(true)
            {
                str += str + new Random().nextInt(88888888)
                        + new Random().nextInt(999999999);
            }
        }
    }
    public class Demo1
    {
    public static void main(String[] args)
    {
    long totalMemory = Runtime.getRuntime().totalMemory(); //JVM中的初始内存总量
    long maxMemory = Runtime.getRuntime().maxMemory(); //JVM试图使用的最大内存

    System.out.println("totalMemory = " + totalMemory + "Byte " +
    (totalMemory / (double) 1024 / 1024) + " MB");
    System.out.println("MaxMemory = " + maxMemory + " Byte " +
    (maxMemory / (double) 1024 / 1024) + " MB");
    }
    }
  • 相关阅读:
    Linux基础网络设置
    CentOS安装
    一.Linux常用命令
    C# 的 ListView 中 多个 Items 的 批量移动方法
    用 Directory.GetFiles 过滤多种类型的文件
    Visual Studio 返回上次编辑位置的快捷键
    C# 的 DataGripView 在大数据时,如何加速?
    webapi和mvc 路由详解
    C# 实体类和DataTable相互转换
    WinForm的DataGridView 下拉滚动条消失
  • 原文地址:https://www.cnblogs.com/yufeng218/p/9159394.html
Copyright © 2011-2022 走看看