多线程--JVM高并发

JVM 常用参数

2019-02-28  本文已影响0人  SonyaBaby

Trace跟踪参数

-verbose:gc
-XX:+PrintGC

[GC (System.gc())  2663K->640K(125952K), 0.0097324 secs]
[Full GC (System.gc())  640K->569K(125952K), 0.0052535 secs]
GC前用了约2663K-> GC后只用了640K(堆的大小约120M)

-XX:+ PrintGCDetails 打印GC详细信息。

[GC (System.gc()) [PSYoungGen: 2663K->616K(38400K)] 2663K->624K(125952K), 0.0036339 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[Full GC (System.gc()) Disconnected from the target VM, address: '127.0.0.1:57922', transport: 'socket'
[PSYoungGen: 616K->0K(38400K)] [ParOldGen: 8K->569K(87552K)] 624K->569K(125952K), [Metaspace: 2830K->2830K(1056768K)], 0.0061896 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
Heap
 PSYoungGen      total 38400K, used 333K [0x00000000d5d80000, 0x00000000d8800000, 0x0000000100000000)
  eden space 33280K, 1% used [0x00000000d5d80000,0x00000000d5dd34a8,0x00000000d7e00000)
  from space 5120K, 0% used [0x00000000d7e00000,0x00000000d7e00000,0x00000000d8300000)
  to   space 5120K, 0% used [0x00000000d8300000,0x00000000d8300000,0x00000000d8800000)
 ParOldGen       total 87552K, used 569K [0x0000000081800000, 0x0000000086d80000, 0x00000000d5d80000)
  object space 87552K, 0% used [0x0000000081800000,0x000000008188e540,0x0000000086d80000)
 Metaspace       used 2839K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 305K, capacity 386K, committed 512K, reserved 1048576K

-XX: + PrintGCTimeStamps 打印GC发的时间戳,自JVM启动以后以秒计量。

0.213: [GC (System.gc())  2663K->656K(125952K), 0.0134724 secs]
0.226: [Full GC (System.gc())  656K->569K(125952K), 0.0212011 secs]

-XX:+PrintGCDateStamps // GC发生的当前时间

2019-02-27T20:21:06.730+0800: [GC (System.gc())  2663K->632K(125952K), 0.0055284 secs]
2019-02-27T20:21:06.736+0800: [Full GC (System.gc())  632K->569K(125952K), 0.0045613 secs]

-XX: +PrintHeapAtGC 每次GC 后,都打印堆的详细信息。Heap before GC ,Heap after GC

{Heap before GC invocations=1 (full 0):
 PSYoungGen      total 38400K, used 2663K [0x00000000d5d80000, 0x00000000d8800000, 0x0000000100000000)
  eden space 33280K, 8% used [0x00000000d5d80000,0x00000000d6019dd0,0x00000000d7e00000)
  from space 5120K, 0% used [0x00000000d8300000,0x00000000d8300000,0x00000000d8800000)
  to   space 5120K, 0% used [0x00000000d7e00000,0x00000000d7e00000,0x00000000d8300000)
 ParOldGen       total 87552K, used 0K [0x0000000081800000, 0x0000000086d80000, 0x00000000d5d80000)
  object space 87552K, 0% used [0x0000000081800000,0x0000000081800000,0x0000000086d80000)
 Metaspace       used 2827K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 304K, capacity 386K, committed 512K, reserved 1048576K
[GC (System.gc())  2663K->704K(125952K), 0.0007834 secs]
Heap after GC invocations=1 (full 0):
 PSYoungGen      total 38400K, used 696K [0x00000000d5d80000, 0x00000000d8800000, 0x0000000100000000)
  eden space 33280K, 0% used [0x00000000d5d80000,0x00000000d5d80000,0x00000000d7e00000)
  from space 5120K, 13% used [0x00000000d7e00000,0x00000000d7eae010,0x00000000d8300000)
  to   space 5120K, 0% used [0x00000000d8300000,0x00000000d8300000,0x00000000d8800000)
 ParOldGen       total 87552K, used 8K [0x0000000081800000, 0x0000000086d80000, 0x00000000d5d80000)
  object space 87552K, 0% used [0x0000000081800000,0x0000000081802000,0x0000000086d80000)
 Metaspace       used 2827K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 304K, capacity 386K, committed 512K, reserved 1048576K
}

XX:+PrintGCApplicationStoppedTime // 输出GC造成应用暂停的时间

Total time for which application threads were stopped: 0.0002322 seconds, Stopping threads took: 0.0001973 seconds
Total time for which application threads were stopped: 0.0000391 seconds, Stopping threads took: 0.0000119 seconds
Total time for which application threads were stopped: 0.0105439 seconds, Stopping threads took: 0.0000160 seconds

监控类的加载

-XX: +TraceClassLoading

[Opened D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.Object from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.io.Serializable from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.Comparable from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.CharSequence from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.String from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.reflect.AnnotatedElement from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.reflect.GenericDeclaration from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.reflect.Type from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
[Loaded java.lang.Class from D:\Java\jdk1.8.0_74\jre\lib\rt.jar]
...

-XX: +PrintClassHistogram 按下Ctrl+Break后。打印类的使用情况 (怎么操作???)

输出日志

-Xloggc:logs/gc.log 指定GC log的路径输出日志文件

-XX:+PrintTenuringDistribution了解新域的情况,了解获得使用期的对象权。(不太懂)

Desired survivor size 5242880 bytes, new threshold 7 (max 15)

堆的分配参数

-Xms:指定最小堆大小。初始堆大小,默认为物理内存的1/64(<1GB);默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,JVM就会增大堆直到-Xmx的最大限制
-Xmx:指定最大堆大小。默认(MaxHeapFreeRatio参数可以调整)空余堆内存大于70%时,JVM会减少堆直到 -Xms的最小限制

public static void printXmxXms(){
  System.out.print("Xmx=");
  System.out.println(Runtime.getRuntime().maxMemory()/1024/1024 + "M");
  System.out.print("free mem=");
  System.out.println(Runtime.getRuntime().freeMemory()/1024/1024 + "M");
  System.out.print("total mem=");
  System.out.println(Runtime.getRuntime().totalMemory()/1024/1024 + "M");
}
Xmx=18M      // 最大可使用空间
free mem=4M  // 当前未使用的空间
total mem=5M // 当前可用总mem空间
public static void printXmxXms(){
  byte[] b = new byte[1024*1024];
  System.out.println("分配了1M空间给数组");
  System.out.print("Xmx=");
  System.out.println(Runtime.getRuntime().maxMemory()/1024/1024 + "M");
  System.out.print("free mem=");
  System.out.println(Runtime.getRuntime().freeMemory()/1024/1024 + "M");
  System.out.print("total mem=");
  System.out.println(Runtime.getRuntime().totalMemory()/1024/1024 + "M");
}
分配了1M空间给数组
Xmx=18M
free mem=3M
total mem=5M
  public static void printXmxXms(){
    byte[] b = new byte[4*1024*1024];
    System.out.println("分配了4M空间给数组");
    System.out.print("Xmx=");
    System.out.println(Runtime.getRuntime().maxMemory()/1024/1024 + "M");
    System.out.print("free mem=");
    System.out.println(Runtime.getRuntime().freeMemory()/1024/1024 + "M");
    System.out.print("total mem=");
    System.out.println(Runtime.getRuntime().totalMemory()/1024/1024 + "M");
  }
分配了4M空间给数组
Xmx=18M
free mem=5M
total mem=10M
  public static void printXmxXms(){
    byte[] b = new byte[4*1024*1024];
    System.out.println("分配了4M空间给数组");
    System.gc();
    System.out.println("回收内存");
    System.out.print("Xmx=");
    System.out.println(Runtime.getRuntime().maxMemory()/1024/1024 + "M");
    System.out.print("free mem=");
    System.out.println(Runtime.getRuntime().freeMemory()/1024/1024 + "M");
    System.out.print("total mem=");
    System.out.println(Runtime.getRuntime().totalMemory()/1024/1024 + "M");
  }
分配了4M空间给数组
回收内存
Xmx=18M
free mem=5M
total mem=10M

-Xmx 和 -Xms保持什么关系,可以让系统的性能尽可能的好?

-Xmn:设置新生代的内存空间大小。注意:此处的大小是(eden+ 2 survivor space)。与jmap -heap中显示的New gen是不同的。整个堆大小=新生代大小 + 老生代大小 + 永久代大小。
在保证堆大小不变的情况下,增大新生代后,将会减小老生代大小。此值对系统性能影响较大,Sun官方推荐配置为整个堆的3/8。

-XX:NewRatio: 老年代(不包含永久区):新生代(eden + 2*s)。例如4,即新生代:老年代 = 1:4,即年轻代占堆的1/5

-XX:SurvivorRatio:新生代中Eden:Survivor容量比值,默认值为8,即两个Survivor区与一个Eden区的比值为2:8,一个Survivor区占整个年轻代的1/10。

-Xss:每个线程的堆栈大小。JDK5.0以后每个线程堆栈大小为1M,以前每个线程堆栈大小为256K。应根据应用的线程所需内存大小进行适当调整。在相同物理内存下,减小这个值能生成更多的线程。但是操作系统对一个进程内的线程数还是有限制的,不能无限生成,经验值在3000~5000左右。一般小的应用, 如果栈不是很深, 应该是128k够用的,大的应用建议使用256k。这个选项对性能影响比较大,需要严格的测试。和threadstacksize选项解释很类似,官方文档似乎没有解释,在论坛中有这样一句话:"-Xss is translated in a VM flag named ThreadStackSize”一般设置这个值就可以了。

  // 去掉局部变量 调用层次可以更深
  public static void testStackDeep(long a, long b, long c){
    long e=1,f=2,g=3,h=4,i=5,k=6,q=7,x=8,y=9,z=10;
    count++;
    testStackDeep(a,b,c);
  }
deep of calling:306
java.lang.StackOverflowError
deep of calling:761
java.lang.StackOverflowError
deep of calling:451
java.lang.StackOverflowError

-XX:PermSize:设置永久代(perm gen)初始值。默认值为物理内存的1/64。

-XX:MaxPermSize:设置持久代最大值。物理内存的1/4。

例子1:Xmn1m
-Xmx20m -Xms20M -Xmn1m -XX:+PrintGCDetails

    byte[] b=null;
    for(int i=0;i<10;i++)
      b = new byte[1024*1024];

Heap
 PSYoungGen      total 1536K, used 928K [0x00000000ffe00000, 0x0000000100000000, 0x0000000100000000)
  eden space 1024K, 90% used [0x00000000ffe00000,0x00000000ffee82f8,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 18432K, used 10240K [0x00000000fec00000, 0x00000000ffe00000, 0x00000000ffe00000)
  object space 18432K, 55% used [0x00000000fec00000,0x00000000ff6000a0,0x00000000ffe00000)
 Metaspace       used 2840K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 305K, capacity 386K, committed 512K, reserved 1048576K

例子2: Xmn15m
-Xmx20m -Xms20M -Xmn15m -XX:+PrintGCDetails

Heap
 PSYoungGen      total 13824K, used 12025K [0x00000000ff100000, 0x0000000100000000, 0x0000000100000000)
  eden space 12288K, 97% used [0x00000000ff100000,0x00000000ffcbe598,0x00000000ffd00000)
  from space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
  to   space 1536K, 0% used [0x00000000ffd00000,0x00000000ffd00000,0x00000000ffe80000)
 ParOldGen       total 5120K, used 0K [0x00000000fec00000, 0x00000000ff100000, 0x00000000ff100000)
  object space 5120K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ff100000)
 Metaspace       used 2835K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 305K, capacity 386K, committed 512K, reserved 1048576K

例子3: Xmn7m
-Xmx20m -Xms20M -Xmn7m -XX:+PrintGCDetails

[GC (Allocation Failure) [PSYoungGen: 5266K->488K(6656K)] 5266K->1616K(19968K), 0.0015067 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 5726K->504K(6656K)] 6854K->2680K(19968K), 0.0038591 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 6656K, used 1589K [0x00000000ff900000, 0x0000000100000000, 0x0000000100000000)
  eden space 6144K, 17% used [0x00000000ff900000,0x00000000ffa0f748,0x00000000fff00000)
  from space 512K, 98% used [0x00000000fff80000,0x00000000ffffe030,0x0000000100000000)
  to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 ParOldGen       total 13312K, used 2176K [0x00000000fec00000, 0x00000000ff900000, 0x00000000ff900000)
  object space 13312K, 16% used [0x00000000fec00000,0x00000000fee20020,0x00000000ff900000)
 Metaspace       used 2835K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 305K, capacity 386K, committed 512K, reserved 1048576K

例子4: -XX:SurvivorRatio=2
-Xmx20m -Xms20M -Xmn7m -XX:SurvivorRatio=2 -XX:+PrintGCDetails
调整幸存代的比例

[GC (Allocation Failure) [PSYoungGen: 3120K->1512K(5632K)] 3120K->1632K(18944K), 0.0011179 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 4662K->1528K(5632K)] 4782K->1648K(18944K), 0.0009248 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 4670K->1512K(5632K)] 4790K->1656K(18944K), 0.0006121 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 5632K, used 3715K [0x00000000ff900000, 0x0000000100000000, 0x0000000100000000)
  eden space 4096K, 53% used [0x00000000ff900000,0x00000000ffb26f98,0x00000000ffd00000)
  from space 1536K, 98% used [0x00000000ffd00000,0x00000000ffe7a030,0x00000000ffe80000)
  to   space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
 ParOldGen       total 13312K, used 144K [0x00000000fec00000, 0x00000000ff900000, 0x00000000ff900000)
  object space 13312K, 1% used [0x00000000fec00000,0x00000000fec24000,0x00000000ff900000)
 Metaspace       used 2836K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 305K, capacity 386K, committed 512K, reserved 1048576K

例子5:-XX:NewRatio 继续增大from to
-Xmx20m -Xms20M -Xmn7m -XX:NewRatio=1 -XX:SurvivorRatio=2 -XX:+PrintGCDetails

[GC (Allocation Failure) [PSYoungGen: 4215K->1720K(7680K)] 4215K->1728K(17920K), 0.0015462 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
[GC (Allocation Failure)[PSYoungGen: 5914K->1720K(7680K)] 5922K->1736K(17920K), 0.0086064 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
Heap
 PSYoungGen      total 7680K, used 5036K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 5120K, 64% used [0x00000000ff600000,0x00000000ff93d270,0x00000000ffb00000)
  from space 2560K, 67% used [0x00000000ffd80000,0x00000000fff2e020,0x0000000100000000)
  to   space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
 ParOldGen       total 10240K, used 16K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 0% used [0x00000000fec00000,0x00000000fec04000,0x00000000ff600000)
 Metaspace       used 2840K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 305K, capacity 386K, committed 512K, reserved 1048576K

例子6:-XX:SurvivorRatio=4 减小幸存代大小增大eden

[GC (Allocation Failure) [PSYoungGen: 6411K->1528K(8704K)] 6411K->1664K(18944K), 0.0009235 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
Heap
 PSYoungGen      total 8704K, used 6847K [0x00000000ff600000, 0x0000000100000000, 0x0000000100000000)
  eden space 7168K, 74% used [0x00000000ff600000,0x00000000ffb31cd8,0x00000000ffd00000)
  from space 1536K, 99% used [0x00000000ffd00000,0x00000000ffe7e020,0x00000000ffe80000)
  to   space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
 ParOldGen       total 10240K, used 136K [0x00000000fec00000, 0x00000000ff600000, 0x00000000ff600000)
  object space 10240K, 1% used [0x00000000fec00000,0x00000000fec22000,0x00000000ff600000)
 Metaspace       used 2836K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 305K, capacity 386K, committed 512K, reserved 1048576K

-XX:+HeapDumpOnOutOfMemoryError OOM时导出堆信息到文件
-XX:+HeapDumpPath 导出OOM的路径

    Vector vector = new Vector();
    for (int i = 0;i<25;i++){
      vector.add(new byte[1024*1024]);
    }
java.lang.OutOfMemoryError: Java heap space
Dumping heap to D:/testHeapDump.dump ...
Disconnected from the target VM, address: '127.0.0.1:60500', transport: 'socket'
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at LookForClassLoader.testHeapDump(LookForClassLoader.java:66)
    at LookForClassLoader.main(LookForClassLoader.java:22)
Heap dump file created [15981435 bytes in 0.027 secs]
.dump.png

通过debug工具打开即可

-XX:OnOutOfMemoryError

堆的分配参数-总结


永久区分配参数

-XX:PermSize -XX:MaxPermSize

例子:使用CGLIB等库的时候,可能会产生大量的类,这些类,有可能撑爆永久区导致OOM

打开堆的Dump


相关了解:

参考文档:https://blog.csdn.net/jisuanjiguoba/article/details/80156781

上一篇下一篇

猜你喜欢

热点阅读