基于参数设置JVM内存空间

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/14988403.html

SRC

package org.fool.test;

import com.sun.management.OperatingSystemMXBean;

import java.lang.management.ManagementFactory;

public class TestJVM {
    public static void main(String[] args) {
        int byteToMB = 1024 * 1024;

        long totalMemory = Runtime.getRuntime().totalMemory() / byteToMB;
        long maxMemory = Runtime.getRuntime().maxMemory() / byteToMB;

        System.out.println("current init memory -Xms: " + totalMemory + " MB");
        System.out.println("current max memory -Xmx: " + maxMemory + " MB");

        OperatingSystemMXBean os = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        long freePhysicalMemory = os.getFreePhysicalMemorySize() / byteToMB;
        long totalPhysicalMemory = os.getTotalPhysicalMemorySize() / byteToMB;
        long usedPhysicalMemory = totalPhysicalMemory - freePhysicalMemory;

        System.out.println("os physical used memory: " + usedPhysicalMemory + " MB");
        System.out.println("os physical free memory: " + freePhysicalMemory + " MB");
        System.out.println("os physical total memory: " + totalPhysicalMemory + " MB");

        System.out.println("default heap init memory: physical memory size / 64 = " + totalPhysicalMemory / 64 + " MB");
        System.out.println("default heap max memory: physical memory size / 4 = " + totalPhysicalMemory / 4 + " MB");
    }
}

默认不设置任何参数运行(本机的内存大小是16G)

自定义设置VM Options

-Xms4096m -Xmx4096m

再次运行


欢迎点赞关注和转发

强者自救 圣者渡人
原文地址:https://www.cnblogs.com/agilestyle/p/14988403.html