Linux java jvm 启动默认内存最大值-Xmx调整为物理内存的80%脚本

java -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintHeapAtGC -XX:+PrintGCTimeStamps -XX:-PrintCommandLineFlags -Xmx`free -m | grep Mem\: | awk '{print $2}' | xargs -i expr {} \* 8 / 10`m  com.hknaruto.cmdApp.Main

部分输出

[root@bogon ~]# java -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintHeapAtGC -XX:+PrintGCTimeStamps -XX:-PrintCommandLineFlags -Xmx`free -m | grep Mem\: | awk '{print $2}' | xargs -i expr {} \* 8 / 10`m  com.hknaruto.cmdApp.Main
current:1MB
{Heap before GC invocations=0 (full 0):
 def new generation   total 2432K, used 1535K [0x00000000e7a00000, 0x00000000e7ca0000, 0x00000000efc00000)
  eden space 2176K,  70% used [0x00000000e7a00000, 0x00000000e7b7fe00, 0x00000000e7c20000)
  from space 256K,   0% used [0x00000000e7c20000, 0x00000000e7c20000, 0x00000000e7c60000)
  to   space 256K,   0% used [0x00000000e7c60000, 0x00000000e7c60000, 0x00000000e7ca0000)
 tenured generation   total 5504K, used 0K [0x00000000efc00000, 0x00000000f0160000, 0x0000000100000000)
   the space 5504K,   0% used [0x00000000efc00000, 0x00000000efc00000, 0x00000000efc00200, 0x00000000f0160000)
 Metaspace       used 2626K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 283K, capacity 386K, committed 512K, reserved 1048576K
0.312: [GC (Allocation Failure) 0.313: [DefNew: 1535K->255K(2432K), 0.0018748 secs] 1535K->1329K(7936K), 0.0019216 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap after GC invocations=1 (full 0):
 def new generation   total 2432K, used 255K [0x00000000e7a00000, 0x00000000e7ca0000, 0x00000000efc00000)
  eden space 2176K,   0% used [0x00000000e7a00000, 0x00000000e7a00000, 0x00000000e7c20000)
  from space 256K,  99% used [0x00000000e7c60000, 0x00000000e7c9fff8, 0x00000000e7ca0000)
  to   space 256K,   0% used [0x00000000e7c20000, 0x00000000e7c20000, 0x00000000e7c60000)
 tenured generation   total 5504K, used 1073K [0x00000000efc00000, 0x00000000f0160000, 0x0000000100000000)
   the space 5504K,  19% used [0x00000000efc00000, 0x00000000efd0c548, 0x00000000efd0c600, 0x00000000f0160000)
 Metaspace       used 2626K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 283K, capacity 386K, committed 512K, reserved 1048576K
}
current:2MB
current:3MB
{Heap before GC invocations=1 (full 0):
 def new generation   total 2432K, used 2346K [0x00000000e7a00000, 0x00000000e7ca0000, 0x00000000efc00000)
  eden space 2176K,  96% used [0x00000000e7a00000, 0x00000000e7c0a978, 0x00000000e7c20000)
  from space 256K,  99% used [0x00000000e7c60000, 0x00000000e7c9fff8, 0x00000000e7ca0000)
  to   space 256K,   0% used [0x00000000e7c20000, 0x00000000e7c20000, 0x00000000e7c60000)
 tenured generation   total 5504K, used 1073K [0x00000000efc00000, 0x00000000f0160000, 0x0000000100000000)
   the space 5504K,  19% used [0x00000000efc00000, 0x00000000efd0c548, 0x00000000efd0c600, 0x00000000f0160000)
 Metaspace       used 2627K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 283K, capacity 386K, committed 512K, reserved 1048576K
0.336: [GC (Allocation Failure) 0.336: [DefNew: 2346K->0K(2432K), 0.0040975 secs] 3419K->3377K(7936K), 0.0041560 secs] [Times: user=0.00 sys=0.01, real=0.01 secs] 
Heap after GC invocations=2 (full 0):
 def new generation   total 2560K, used 0K [0x00000000e7a00000, 0x00000000e7cc0000, 0x00000000efc00000)
  eden space 2304K,   0% used [0x00000000e7a00000, 0x00000000e7a00000, 0x00000000e7c40000)
  from space 256K,   0% used [0x00000000e7c40000, 0x00000000e7c40000, 0x00000000e7c80000)
  to   space 256K,   0% used [0x00000000e7c80000, 0x00000000e7c80000, 0x00000000e7cc0000)
 tenured generation   total 5504K, used 3377K [0x00000000efc00000, 0x00000000f0160000, 0x0000000100000000)
   the space 5504K,  61% used [0x00000000efc00000, 0x00000000eff4c4c0, 0x00000000eff4c600, 0x00000000f0160000)
 Metaspace       used 2627K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 283K, capacity 386K, committed 512K, reserved 1048576K
}
current:4MB
current:5MB




略。。。。。




java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid3388.hprof ...
Heap dump file created [393060783 bytes in 7.119 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at com.hknaruto.cmdApp.Main.main(Main.java:16)
Heap
 def new generation   total 119808K, used 118785K [0x00000000e7a00000, 0x00000000efc00000, 0x00000000efc00000)
  eden space 106496K, 100% used [0x00000000e7a00000, 0x00000000ee200000, 0x00000000ee200000)
  from space 13312K,  92% used [0x00000000eef00000, 0x00000000efb00448, 0x00000000efc00000)
  to   space 13312K,   0% used [0x00000000ee200000, 0x00000000ee200000, 0x00000000eef00000)
 tenured generation   total 266240K, used 265511K [0x00000000efc00000, 0x0000000100000000, 0x0000000100000000)
   the space 266240K,  99% used [0x00000000efc00000, 0x00000000fff49e88, 0x00000000fff4a000, 0x0000000100000000)
 Metaspace       used 2697K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 287K, capacity 386K, committed 512K, reserved 1048576K




测试源码

package com.hknaruto.cmdApp;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        long buffSize = 0;
        int blockSize = 1024 * 1024;
        List list = new ArrayList<>();
        while (true) {
            try {
                byte[] buff = new byte[blockSize];
                list.add(buff);
                buffSize += blockSize;
                System.out.println("current:" + buffSize / 1024 / 1024 + "MB");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

 

你可能感兴趣的:(java)