java jvm及gc參數設置


通用類

package lddxfs.jvm.gclog;

import java.util.HashMap;
import java.util.Map;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/15
 */
public class Common {
    public static Map<Integer, byte[]> map = new HashMap<>();

    public static void gcTest(String[] args) {
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                map.put(i, new byte[1 * 1024 * 1024]);
            }
            map.clear();
        }
        map.clear();
    }
}
View Code

-XX:+PrintGC 打印GC的概要信息

package lddxfs.jvm.gclog;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/15
 */
public class PrintGC {
    // verbose:gc
// -XX:+PrintGC
// 可以打印GC的簡要信息
    public static void main(String[] args) {
       Common.gcTest(args);
    }
    /**
     [GC (Allocation Failure)  64635K->1936K(251392K), 0.0010561 secs]
     [GC (Allocation Failure)  66695K->1904K(251392K), 0.0011375 secs]
     [GC (Allocation Failure)  66950K->1840K(251392K), 0.0006178 secs]
     [GC (Allocation Failure)  66602K->1808K(316928K), 0.0009353 secs]
     [GC (Allocation Failure)  132379K->1864K(316928K), 0.0012672 secs]
     */
}
View Code

-XX:+PrintGCDetails 可以打印GC的詳細信息

package lddxfs.jvm.gclog;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/15
 */
public class PrintGCDetails {
    // -XX:+PrintGCDetails
// 可以打印GC的詳細信息
    public static void main(String[] args) {
        Common.gcTest(args);
    }
    /**
     [GC (Allocation Failure) [PSYoungGen: 64635K->1864K(76288K)] 64635K->1872K(251392K), 0.0013503 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     [GC (Allocation Failure) [PSYoungGen: 66623K->1880K(141824K)] 66631K->1888K(316928K), 0.0009000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     [GC (Allocation Failure) [PSYoungGen: 131972K->1800K(141824K)] 131980K->1808K(316928K), 0.0012225 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     [GC (Allocation Failure) [PSYoungGen: 132342K->1880K(272896K)] 132350K->1896K(448000K), 0.0009099 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     Heap
     PSYoungGen      total 272896K, used 33675K [0x000000076af00000, 0x000000077c400000, 0x00000007c0000000)
     eden space 262144K, 12% used [0x000000076af00000,0x000000076ce0cc48,0x000000077af00000)
     from space 10752K, 17% used [0x000000077b980000,0x000000077bb56020,0x000000077c400000)
     to   space 10752K, 0% used [0x000000077af00000,0x000000077af00000,0x000000077b980000)
     ParOldGen       total 175104K, used 16K [0x00000006c0c00000, 0x00000006cb700000, 0x000000076af00000)
     object space 175104K, 0% used [0x00000006c0c00000,0x00000006c0c04000,0x00000006cb700000)
     Metaspace       used 3455K, capacity 4500K, committed 4864K, reserved 1056768K
     class space    used 376K, capacity 388K, committed 512K, reserved 1048576K
     */
}
View Code

-XX:+PrintGCTimeStamps -XX:+PrintGCDetails 打印GC發生的時間戳

package lddxfs.jvm.gclog;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/15
 */
public class PrintGCTimeStamps {
    //-XX:+PrintGCTimeStamps -XX:+PrintGCDetails
    // 打印GC發生的時間戳
    public static void main(String[] args) {
        Common.gcTest(args);
    }
    /**
     0.108: [GC (Allocation Failure) [PSYoungGen: 64635K->1896K(76288K)] 64635K->1904K(251392K), 0.0013443 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     0.112: [GC (Allocation Failure) [PSYoungGen: 66655K->1880K(141824K)] 66663K->1888K(316928K), 0.0008619 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     0.133: [GC (Allocation Failure) [PSYoungGen: 131972K->1832K(141824K)] 131980K->1840K(316928K), 0.0012186 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     0.140: [GC (Allocation Failure) [PSYoungGen: 132374K->1800K(272896K)] 132382K->1816K(448000K), 0.0008880 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     Heap
     PSYoungGen      total 272896K, used 33595K [0x000000076af00000, 0x000000077c400000, 0x00000007c0000000)
     eden space 262144K, 12% used [0x000000076af00000,0x000000076ce0cc70,0x000000077af00000)
     from space 10752K, 16% used [0x000000077b980000,0x000000077bb42030,0x000000077c400000)
     to   space 10752K, 0% used [0x000000077af00000,0x000000077af00000,0x000000077b980000)
     ParOldGen       total 175104K, used 16K [0x00000006c0c00000, 0x00000006cb700000, 0x000000076af00000)
     object space 175104K, 0% used [0x00000006c0c00000,0x00000006c0c04000,0x00000006cb700000)
     Metaspace       used 3454K, capacity 4500K, committed 4864K, reserved 1056768K
     class space    used 376K, capacity 388K, committed 512K, reserved 1048576K
     */


}
View Code

-XX:+PrintHeapAtGC 每次CG后都打印堆棧信息

package lddxfs.jvm.gclog;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/15
 */
public class PrintHeapAtGC {

    //每次CG后都打印堆棧信息
    //-XX:+PrintHeapAtGC
    public static void main(String[] args) {
        Common.gcTest(args);
    }

    /**
     * {Heap before GC invocations=1 (full 0):
     *  PSYoungGen      total 76288K, used 64635K [0x000000076af00000, 0x0000000770400000, 0x00000007c0000000)
     *   eden space 65536K, 98% used [0x000000076af00000,0x000000076ee1efb0,0x000000076ef00000)
     *   from space 10752K, 0% used [0x000000076f980000,0x000000076f980000,0x0000000770400000)
     *   to   space 10752K, 0% used [0x000000076ef00000,0x000000076ef00000,0x000000076f980000)
     *  ParOldGen       total 175104K, used 0K [0x00000006c0c00000, 0x00000006cb700000, 0x000000076af00000)
     *   object space 175104K, 0% used [0x00000006c0c00000,0x00000006c0c00000,0x00000006cb700000)
     *  Metaspace       used 3441K, capacity 4500K, committed 4864K, reserved 1056768K
     *   class space    used 375K, capacity 388K, committed 512K, reserved 1048576K
     * Heap after GC invocations=1 (full 0):
     *  PSYoungGen      total 76288K, used 1864K [0x000000076af00000, 0x0000000774400000, 0x00000007c0000000)
     *   eden space 65536K, 0% used [0x000000076af00000,0x000000076af00000,0x000000076ef00000)
     *   from space 10752K, 17% used [0x000000076ef00000,0x000000076f0d2030,0x000000076f980000)
     *   to   space 10752K, 0% used [0x0000000773980000,0x0000000773980000,0x0000000774400000)
     *  ParOldGen       total 175104K, used 8K [0x00000006c0c00000, 0x00000006cb700000, 0x000000076af00000)
     *   object space 175104K, 0% used [0x00000006c0c00000,0x00000006c0c02000,0x00000006cb700000)
     *  Metaspace       used 3441K, capacity 4500K, committed 4864K, reserved 1056768K
     *   class space    used 375K, capacity 388K, committed 512K, reserved 1048576K
     * }
     * {Heap before GC invocations=2 (full 0):
     *  PSYoungGen      total 76288K, used 66623K [0x000000076af00000, 0x0000000774400000, 0x00000007c0000000)
     *   eden space 65536K, 98% used [0x000000076af00000,0x000000076ee3df40,0x000000076ef00000)
     *   from space 10752K, 17% used [0x000000076ef00000,0x000000076f0d2030,0x000000076f980000)
     *   to   space 10752K, 0% used [0x0000000773980000,0x0000000773980000,0x0000000774400000)
     *  ParOldGen       total 175104K, used 8K [0x00000006c0c00000, 0x00000006cb700000, 0x000000076af00000)
     *   object space 175104K, 0% used [0x00000006c0c00000,0x00000006c0c02000,0x00000006cb700000)
     *  Metaspace       used 3443K, capacity 4500K, committed 4864K, reserved 1056768K
     *   class space    used 375K, capacity 388K, committed 512K, reserved 1048576K
     * Heap after GC invocations=2 (full 0):
     *  PSYoungGen      total 141824K, used 1880K [0x000000076af00000, 0x0000000774400000, 0x00000007c0000000)
     *   eden space 131072K, 0% used [0x000000076af00000,0x000000076af00000,0x0000000772f00000)
     *   from space 10752K, 17% used [0x0000000773980000,0x0000000773b56020,0x0000000774400000)
     *   to   space 10752K, 0% used [0x0000000772f00000,0x0000000772f00000,0x0000000773980000)
     *  ParOldGen       total 175104K, used 8K [0x00000006c0c00000, 0x00000006cb700000, 0x000000076af00000)
     *   object space 175104K, 0% used [0x00000006c0c00000,0x00000006c0c02000,0x00000006cb700000)
     *  Metaspace       used 3443K, capacity 4500K, committed 4864K, reserved 1056768K
     *   class space    used 375K, capacity 388K, committed 512K, reserved 1048576K
     * }
     */
}
View Code
 
        

-XX:+TraceClassLoading 監控類的加載

package lddxfs.jvm.gclog;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/15
 */
public class TraceClassLoading {
    //-XX:+TraceClassLoading
    //監控類的加載
    public static void main(String[] args) {
        Common.gcTest(args);
    }
    /**
     * ........
     * [Opened D:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar]
     * [Loaded java.lang.Object from D:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar]
     * [Loaded java.io.Serializable from D:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar]
     * [Loaded java.lang.Comparable from D:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar]
     * [Loaded java.lang.CharSequence from D:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar]
     * [Loaded java.lang.String from D:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar]
     * .........
     */
}
View Code

-XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:D:\gc.log 輸出GC文件

package lddxfs.jvm.gclog;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/15
 */
public class Xloggc {
    //輸出GC文件
    //-XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:D:\gc.log
    public static void main(String[] args) {
        Common.gcTest(args);
    }
    /**
     * Java HotSpot(TM) 64-Bit Server VM (25.181-b13) .....
     * CommandLine flags: -XX:InitialHeapSize=267636864 -XX:MaxHeapSize=4282189824 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
     * 0.107: [GC (Allocation Failure) [PSYoungGen: 64635K->1848K(76288K)] 64635K->1856K(251392K), 0.0010243 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.111: [GC (Allocation Failure) [PSYoungGen: 66607K->1848K(76288K)] 66615K->1856K(251392K), 0.0010109 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.115: [GC (Allocation Failure) [PSYoungGen: 66894K->1832K(76288K)] 66902K->1840K(251392K), 0.0007068 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.118: [GC (Allocation Failure) [PSYoungGen: 66594K->1880K(141824K)] 66602K->1888K(316928K), 0.0008178 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * 0.139: [GC (Allocation Failure) [PSYoungGen: 132451K->1816K(141824K)] 132459K->1832K(316928K), 0.0012177 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * Heap
     *  PSYoungGen      total 141824K, used 29993K [0x000000076af00000, 0x000000077b300000, 0x00000007c0000000)
     *   eden space 131072K, 21% used [0x000000076af00000,0x000000076ca84560,0x0000000772f00000)
     *   from space 10752K, 16% used [0x0000000772f00000,0x00000007730c6040,0x0000000773980000)
     *   to   space 2048K, 0% used [0x000000077b100000,0x000000077b100000,0x000000077b300000)
     *  ParOldGen       total 175104K, used 16K [0x00000006c0c00000, 0x00000006cb700000, 0x000000076af00000)
     *   object space 175104K, 0% used [0x00000006c0c00000,0x00000006c0c04000,0x00000006cb700000)
     *  Metaspace       used 3455K, capacity 4500K, committed 4864K, reserved 1056768K
     *   class space    used 376K, capacity 388K, committed 512K, reserved 1048576K
     */
}
View Code

-XX:+HeapDumpOnOutOfMemoryError

package lddxfs.jvm.heap;

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

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/16
 */
public class HeapDumpOnOutOfMemoryError {
    /**
     * <pre>
     *  -XX:+HeapDumpOnOutOfMemoryError
     *   OOM時導出堆到文件
     *   -XX:+HeapDumpPath
     *   導出OOM的路徑
     *   -Xms5m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:/test001/
     *
     * 此外,OnOutOfMemoryError參數允許用戶指定當出現oom時,指定某個腳本來完成一些動作。
     * -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/local/tomcat/ -XX:OnOutOfMemoryError="sh ~/test.sh"
     *
     * 1、配置方法
     *     在JAVA_OPTIONS變量中增加
     *     -XX:+HeapDumpOnOutOfMemoryError  -XX:HeapDumpPath=${目錄}。
     * 2、參數說明
     *    (1)-XX:+HeapDumpOnOutOfMemoryError參數表示當JVM發生OOM時,自動生成DUMP文件。
     *    (2)-XX:HeapDumpPath=${目錄}參數表示生成DUMP文件的路徑,也可以指定文件名稱,例如:-XX:HeapDumpPath=${目錄}/java_heapdump.hprof。
     *    如果不指定文件名,默認為:java_<pid>_<date>_<time>_heapDump.hprof。
     *  </pre>
     */
    public static void main(String[] args) {
        List<byte[]> list=new ArrayList<>();
        for(int i=0;i<20;i++){
          list.add(new byte[1024*1024]);
        }
    }
    /**
     * //-Xms5m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:/test001.dump
     * java.lang.OutOfMemoryError: Java heap space
     * Dumping heap to d:/test001.dump ...
     * Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
     *     at lddxfs.jvm.heap.HeapDumpOnOutOfMemoryError.main(HeapDumpOnOutOfMemoryError.java:19)
     * Heap dump file created [16164232 bytes in 0.015 secs]
     *
     * Process finished with exit code 1
     *
     * 然后使用Eclipse MemoryAnalyzer.exe 打開文件即可分析
     */

}
View Code

-Xmn數字m 設置新生代大小

package lddxfs.jvm.heap;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/15
 */
public class Xmn {
    //-Xms20m -Xmx20m -Xmn1m -XX:+PrintGCDetails
    //指定最小堆 最大堆 設置新生代大小 打印GC詳細
    //-XX:NewRatio
    //新生代(eden+2*s)和老年代(不包含永久區)的比值
    // 4 表示 新生代:老年代 =1:4 即年輕代占的1/5

    //-XX:SurvivorRatio
    //設置兩個Survivor(from to)區和edon的比
    //8 表示 兩個Survivor:eden=2:8 ,即一個Survivor占年輕代的1/10
    public static void main(String[] args) {
        byte[] b;
        for (int i = 0; i < 10; i++) {
            b = new byte[1 * 1024 * 1024];
        }
    }
    /**
     * //-Xms20m -Xmx20m -Xmn1m -XX:+PrintGCDetails
     * 指定最小堆20m,最大堆20m,設置新生代1m
     * [GC (Allocation Failure) [PSYoungGen: 505K->488K(1024K)] 505K->512K(19968K), 0.0005177 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * [GC (Allocation Failure) [PSYoungGen: 1000K->504K(1024K)] 1024K->660K(19968K), 0.0005936 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * [GC (Allocation Failure) [PSYoungGen: 1004K->504K(1024K)] 1160K->724K(19968K), 0.0005464 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * Heap
     *  PSYoungGen      total 1024K, used 902K [0x00000000ffe80000, 0x0000000100000000, 0x0000000100000000)
     *   eden space 512K, 77% used [0x00000000ffe80000,0x00000000ffee3b00,0x00000000fff00000)
     *   from space 512K, 98% used [0x00000000fff00000,0x00000000fff7e010,0x00000000fff80000)
     *   to   space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
     *  ParOldGen       total 18944K, used 10460K [0x00000000fec00000, 0x00000000ffe80000, 0x00000000ffe80000)
     *   object space 18944K, 55% used [0x00000000fec00000,0x00000000ff637100,0x00000000ffe80000)
     *  Metaspace       used 3445K, capacity 4496K, committed 4864K, reserved 1056768K
     *   class space    used 376K, capacity 388K, committed 512K, reserved 1048576K
     * Java HotSpot(TM) 64-Bit Server VM warning: NewSize (1536k) is greater than the MaxNewSize (1024k). A new max generation size of 1536k will be used.
     *
     * Process finished with exit code 0
     */

    /**
     * //-Xms20m -Xmx20m -Xmn15m -XX:+PrintGCDetails
     *  設置最小堆20m,最大堆20m,設置新生代15m, 打印GC詳細
     * [GC (Allocation Failure) [PSYoungGen: 11517K->856K(13824K)] 11517K->864K(18944K), 0.0014970 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * Heap
     *  PSYoungGen      total 13824K, used 2126K [0x00000000ff100000, 0x0000000100000000, 0x0000000100000000)
     *   eden space 12288K, 10% used [0x00000000ff100000,0x00000000ff23d8a0,0x00000000ffd00000)
     *   from space 1536K, 55% used [0x00000000ffd00000,0x00000000ffdd6030,0x00000000ffe80000)
     *   to   space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
     *  ParOldGen       total 5120K, used 8K [0x00000000fec00000, 0x00000000ff100000, 0x00000000ff100000)
     *   object space 5120K, 0% used [0x00000000fec00000,0x00000000fec02000,0x00000000ff100000)
     *  Metaspace       used 3413K, capacity 4496K, committed 4864K, reserved 1056768K
     *   class space    used 370K, capacity 388K, committed 512K, reserved 1048576K
     *
     * Process finished with exit code 0
     */

    /**
     * //-Xms20m -Xmx20m -Xmn7m -XX:+PrintGCDetails
     * 設置最小堆20m,最大堆20m,設置新生代7m, 打印GC詳細
     * [GC (Allocation Failure) [PSYoungGen: 5163K->504K(6656K)] 5163K->712K(19968K), 0.0009239 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * [GC (Allocation Failure) [PSYoungGen: 5803K->488K(6656K)] 6011K->808K(19968K), 0.0004184 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * Heap
     *  PSYoungGen      total 6656K, used 2748K [0x00000000ff900000, 0x0000000100000000, 0x0000000100000000)
     *   eden space 6144K, 36% used [0x00000000ff900000,0x00000000ffb35348,0x00000000fff00000)
     *   from space 512K, 95% used [0x00000000fff80000,0x00000000ffffa020,0x0000000100000000)
     *   to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
     *  ParOldGen       total 13312K, used 320K [0x00000000fec00000, 0x00000000ff900000, 0x00000000ff900000)
     *   object space 13312K, 2% used [0x00000000fec00000,0x00000000fec50010,0x00000000ff900000)
     *  Metaspace       used 3420K, capacity 4496K, committed 4864K, reserved 1056768K
     *   class space    used 371K, capacity 388K, committed 512K, reserved 1048576K
     *
     * Process finished with exit code 0
     */

    /**
     * //-Xms20m -Xmx20m -Xmn7m -XX:SurvivorRatio=2 -XX:+PrintGCDetails
     *  設置最小堆20m,最大堆20m,設置新生代7m,  打印GC詳細
     * [GC (Allocation Failure) [PSYoungGen: 4069K->808K(5632K)] 4069K->816K(18944K), 0.0007074 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * [GC (Allocation Failure) [PSYoungGen: 4000K->792K(5632K)] 4008K->800K(18944K), 0.0005794 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * [GC (Allocation Failure) [PSYoungGen: 4043K->776K(5632K)] 4051K->784K(18944K), 0.0005552 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * Heap
     *  PSYoungGen      total 5632K, used 2955K [0x00000000ff900000, 0x0000000100000000, 0x0000000100000000)
     *   eden space 4096K, 53% used [0x00000000ff900000,0x00000000ffb20ea8,0x00000000ffd00000)
     *   from space 1536K, 50% used [0x00000000ffd00000,0x00000000ffdc2020,0x00000000ffe80000)
     *   to   space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
     *  ParOldGen       total 13312K, used 8K [0x00000000fec00000, 0x00000000ff900000, 0x00000000ff900000)
     *   object space 13312K, 0% used [0x00000000fec00000,0x00000000fec02000,0x00000000ff900000)
     *  Metaspace       used 3425K, capacity 4496K, committed 4864K, reserved 1056768K
     *   class space    used 372K, capacity 388K, committed 512K, reserved 1048576K
     *
     * Process finished with exit code 0
     */

    /**
     * //-Xms20m -Xms20m -XX:NewRatio=1 -XX:SurvivorRatio=2 -XX:+PrintGCDetails
     * [GC (Allocation Failure) [PSYoungGen: 5058K->792K(7680K)] 5058K->800K(17920K), 0.0007751 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * [GC (Allocation Failure) [PSYoungGen: 5038K->744K(7680K)] 5046K->752K(17920K), 0.0006721 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * Heap
     *  PSYoungGen      total 7680K, used 4095K [0x0000000740600000, 0x0000000741500000, 0x00000007c0000000)
     *   eden space 5120K, 65% used [0x0000000740600000,0x0000000740945bf0,0x0000000740b00000)
     *   from space 2560K, 29% used [0x0000000740d80000,0x0000000740e3a020,0x0000000741000000)
     *   to   space 2560K, 0% used [0x0000000740b00000,0x0000000740b00000,0x0000000740d80000)
     *  ParOldGen       total 10240K, used 8K [0x00000006c0c00000, 0x00000006c1600000, 0x0000000740600000)
     *   object space 10240K, 0% used [0x00000006c0c00000,0x00000006c0c02000,0x00000006c1600000)
     *  Metaspace       used 3423K, capacity 4496K, committed 4864K, reserved 1056768K
     *   class space    used 372K, capacity 388K, committed 512K, reserved 1048576K
     *
     * Process finished with exit code 0
     *
     */

    /**
     *  //-Xms20m -Xms20m -XX:NewRatio=1 -XX:SurvivorRatio=8 -XX:+PrintGCDetails
     *[GC (Allocation Failure) [PSYoungGen: 7296K->792K(9216K)] 7296K->800K(19456K), 0.0007097 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
     * Heap
     *  PSYoungGen      total 9216K, used 6234K [0x0000000740600000, 0x0000000741000000, 0x00000007c0000000)
     *   eden space 8192K, 66% used [0x0000000740600000,0x0000000740b509e8,0x0000000740e00000)
     *   from space 1024K, 77% used [0x0000000740e00000,0x0000000740ec6030,0x0000000740f00000)
     *   to   space 1024K, 0% used [0x0000000740f00000,0x0000000740f00000,0x0000000741000000)
     *  ParOldGen       total 10240K, used 8K [0x00000006c0c00000, 0x00000006c1600000, 0x0000000740600000)
     *   object space 10240K, 0% used [0x00000006c0c00000,0x00000006c0c02000,0x00000006c1600000)
     *  Metaspace       used 3399K, capacity 4496K, committed 4864K, reserved 1056768K
     *   class space    used 370K, capacity 388K, committed 512K, reserved 1048576K
     */

}
View Code

-Xms數字m -Xmx數字m 指定最小堆 和 最大堆

package lddxfs.jvm.heap;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/15
 */
public class XmsXmx {
    //-Xms5m -Xmx20m
    //指定最小堆 和 最大堆
    public static void main(String[] args) {
        logMemory(Runtime.getRuntime().maxMemory());
        logMemory(Runtime.getRuntime().freeMemory());
        logMemory(Runtime.getRuntime().totalMemory());
    }

    public static void logMemory(long memory){
        System.out.println(memory/1024.0/1024+"M");
    }
//18M
//4M
//5M
    /**
     * 在Tomcat的catalina.sh文件中的啟停server腳本中都應用到了兩個變量: CATALINA_OPTS和JAVA_OPTS。用於保存Tomcat運行所需的各種參數。
     * 他們在文件中的注釋如下:
     * (可選)Java 執行"start","stop"或"run"命令時用到的運行時參數; [JAVA_OPTS]
     * (可選)Java 執行"start"或"run"命令時用到的運行時參數; [CATALINA_OPTS]
     * 那么,為什么有兩個不同的變量?他們有什么區別?
     * 首先,定義在這兩個變量中的參數都會被傳遞到啟動Tomcat的命令:"start"和"run",只有定義在JAVA_OPTS中的參數會被傳遞到"stop"命令。
     * 所以將參數定義到哪個變量中並不影響Tomcat的啟動和運行,而只影響到了Tomcat的運行結束。
     *
     * 第二種區別更加微妙。其他應用程序也可以使用JAVA_OPTS,但Tomcat只會用到CATALINA_OPTS。所以如果你只使用了Tomcat,在設置環境變量時,你最好使用CATALINA_OPTS,而如果你同時也用到了其他java應用程序,如JBoss,在設置環境變量時你應該使用JAVA_OPTS。
     */
}
View Code

-XX:PermSize -XX:MaxPermSize 設置永久區 設置永久區的初始空間和最大空間,他們表示,一個系統可以容納多少個類型

package lddxfs.jvm.perm;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/16
 */
public class PermSize {
    /**
     * 這里
     * -XX:PermSize -XX:MaxPermSize
     * 設置永久區的初始空間和最大空間,他們表示,一個系統可以容納多少個類型
     */
    public static void main(String[] args) {
       for(int i=0;i<10000;i++){
           Enhancer enhancer=new Enhancer();
           enhancer.setSuperclass(PermSize.class);
           enhancer.setCallback((InvocationHandler) (o, method, objects) -> method.invoke(o,objects));
           enhancer.create();
       }
        /**
         * -XX:PermSize=2M -XX:MaxPermSize=2M
         * //        Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=2M; support was removed in 8.0
         * //        Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=2M; support was removed in 8.0
         */
    }
}
View Code
 
        

-Xss 棧大小分配

 
        
package lddxfs.jvm.xss;

/**
 * Author:lddxfs(lddxfs@qq.com;https://www.cnblogs.com/LDDXFS/)
 * Date:2018/10/16
 */
public class Xss {
    private static int count = 0;

    /**
     * -Xss
     * 棧大小分配
     * 通常只有幾百k
     * 決定了函數調用的深度
     * 每個線程都有獨立的棧空間
     * 局部變量 參數 分配在棧上
     */
    public static void main(String[] args) {
        try {
            recursion(0l, 0l, 0l);

        } catch (Throwable e) {
            System.out.println("deep of calling = " + count);
            e.printStackTrace();
        }
    }

    public static void recursion(long a, long b, long c) {
        long e = 1, f = 2, g = 3, h = 4, i = 5, k = 7, q = 7, x = 8, y = 9, z = 10;
        count++;
        recursion(a, b, c);
    }
    /**
     * //-Xss128K
     *deep of calling = 305
     * java.lang.StackOverflowError
     *     at lddxfs.jvm.xss.Xss.recursion(Xss.java:29)
     *     at lddxfs.jvm.xss.Xss.recursion(Xss.java:31)
     *     at lddxfs.jvm.xss.Xss.recursion(Xss.java:31)
     *     at lddxfs.jvm.xss.Xss.recursion(Xss.java:31)
     */
    /**
     * //-Xss256K
     * deep of calling = 757
     * java.lang.StackOverflowError
     *     at lddxfs.jvm.xss.Xss.recursion(Xss.java:29)
     *     at lddxfs.jvm.xss.Xss.recursion(Xss.java:31)
     *     at lddxfs.jvm.xss.Xss.recursion(Xss.java:31)
     */

}
View Code
 
        

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM