JVM性能调优(京东内部系统高性能优化思路)

一 简介jvm堆和栈空间设置

堆空间:heap=young+old,既:堆空间=年轻代+老年代。其中年轻代:young=2*survivor+eden,既:年轻代=两个survivor空间+1个eden空间。堆空间保存了new出对象和数组的实际数据,也是gc最爱回收的。同时对对空间进行了分代管理:

  • 年轻代:创建的新对象会被放入年轻代的eden空间,而年轻代gc采用复制算法,复制算法会把内存分为两个区域(即两个survivor空间:from和to)。当进行一次minor gc时(既年轻代的gc),minor gc是串行的,eden空间如果没有被gc root引用的会被回收,而依然存活的会被移动到from空间中,如果from空间在minor gc时对象依旧可以存活,就会对该对象年龄+1,当年龄达到一定数值时会直接放入老年代,没有达到年龄的存活对象会被复制到to中。这时from和eden空间已经被清空,虚拟机会交换from和to的空间,空的from变成to,to的变成from,保证了to是空的,minor gc会不断重复这样的工作,直到to彻底被填满,这时会将对象移动到老年代。

  • 老年代:老年代空间的对象是经过minor gc反复锤炼出来的。老年代使用并行的gc回收期,标记-清除算法,并且产生的是full gc(major gc)。老年代gc虽然是并行的,但full gc会同时对年轻代进行gc,所以大量的full gc会严重耗费jvm的性能,甚至卡死应用。另外可以大对象会直接分配到老年代,避免了在minor gc对两个survivor空间的复制耗时。

永久代:也就是方法栈注意java8中永久代被彻底移除了。永久代包含了Class的元信息和常量池。该区域是gc最不爱回收的。

栈空间:分为本地方法栈和虚拟机栈,本地方法栈是被声明为native的方法存放的空间。虚拟机栈是我们通常说的栈,线程私有,随着线程销毁而销毁,gc是不管这里的。包含了:局部变量表、操作数栈、动态链表、方法出口信息等。我们常用的hotspot把本地方法栈和虚拟机栈合成了一个。

  • -Xmx:最大堆内存,如 java -Xmx1024m
  • -Xms:初始化堆内存大小,如 java -Xmx1024m -Xms1024m,注意如果这最大和初始化堆大小设置相同的话,可以防止jvm的堆内存动态扩容
  • -Xmn:年轻代空间大小,如java -Xmx1024m -Xms1024m -Xmn256m
  • -Xss:线程栈大小,如java -Xmx1024m -Xms1024m -Xmn256m -Xss128k,注意jdk1.5之前每个线程栈默认为256k,之后是1m,越多的线程栈空间能换取的线程数越少,反之越少的线程栈空间能换取的线程数越多
  • -Xoss:本地方法栈大小,对hotspot无效
  • -XX:NewRatio:设置年轻代与老年代的比例,如java -Xmx1024m -Xms1024m -Xss128k -XX:NewRatio=4
  • -XX:SurvivorRatio:设置survivor空间占年轻代空间的比例,如:java -Xmx1024m -Xms1024m -Xmn256m -XX:SurvivorRatio=4,eden:survivor=4:2
  • -XX:MaxPermSize:永久代空间大小,如:java -Xmx1024m -Xms1024m -Xss128k -XX:MaxPermSize=16m
  • -XX:MaxTenuringThreshold:年轻代最大gc年龄,如果超过这个阈值会直接接入老年代,如果设置为0,年轻代不经过survivor空间直接进入老年代,如:java -Xmx1024m -Xms1024m -Xss128k -XX:MaxTenuringThreshold=0
  • -XX:PretenureSizeThreshold:设置大对象直接进入老年代的阈值,当大对象大小超过该值将会直接在老年代分配。如:java -Xmx1024m -Xms1024m -XX:PretenureSizeThreshold=5242880

二 垃圾收集器设置


在jdk1.6中提供的gc年轻代分为:Serial、Parallel Scavenge、ParNew,而老年代分为:Serial、Parallel、CMS。在jdk1.7中加入了G1。其中Serial为串行gc,是jvm参数指定-client时使用的默认gc。Parallel和ParNew为串行gc,jvm参数指定-server时使用的默认gc为Parallel Scavenge,同样CMS为老年代并行gc,G1为jdk1.7实验性gc,为java9默认的gc。
串行gc在垃圾回收时会stop the world,既停止当前用户线程,然后进行垃圾回收,这样做的目的是防止用户线程继续运行产生内存碎片。而串行gc在垃圾回收时通常不会stop the world,而CMS gc会进行多次垃圾回收(期间会进行一次短暂的stop the world)或者压缩来减少内存碎片。
另外Parallel Scavenge和ParNew的区别在于Parallel Scavenge更关注与吞吐量,既吞吐量配置参数可控。
G1垃圾回收器将内存从分代转换为分块,将内存分配为多块大小相等的heap,每个heap有独立的eden、survivor、old空间,在内存逻辑上都是连续的。采用并行标记压缩方式。

  • -XX:+UseSerialGC:在新生代和老年代使用串休gc
  • -XX:+UseParNewGc:在新生代使用并行gc
  • -XX:+UseParallelOldGC:在老年代使用并行gc
  • -XX:ParallelGCThread:设置Parallel gc的垃圾回收线程数,通常与cpu数量相同
  • -XX:MaxGCPauseMillis:设置最大垃圾收集停顿时间,垃圾回收器会尽量控制回收的时间在该值范围内
  • -XX:GCPauseIntervalMillis:设置停顿时间间隔
  • -XX:GCTimeRatio:设置吞吐量大小,0~100之间的整数。若该值为n,那么jvm将会花费不超过1/(1+n)的时间用于垃圾回收。
  • -XX:+UseAdaptiveSizePolicy:开启自适应gc策略,jvm会根据运行时吞吐量等信息自动调整eden、old等空间大小以及晋升老年代年龄
  • -XX:+UseConcMarkSweepGC:新生代使用ParNew,老年代使用CMS和Serial,其中老年代的Serial用于作为CMS失败时调用的备选gc
  • -XX:+ParallelCMSThreads:CMS线程数量
  • -XX:CMSInitiatingOccupancyFraction:设置老年代空间被使用多少后触发CMS gc,默认为68%
  • -XX:CMSFullGCsBeforeCompaction:设置多少次CMS回收后,进行一次内存压缩
  • -XX:+CMSClassUnloadingEnabled:在类卸载后进行CMS回收
  • -XX:+CMSParallelRemarkEnabled:启用并行重标记
  • -XX:CMSInitiatingPermOccupancyFraction:当永久代空间被使用多少后触发CMS gc,百分比(在使用时CMSClassUnloadingEnabled必须被配置)
  • UseCMSInitiatingOccupancyOnly:只有当gc达到配置的阈值时才进行回收
  • XX:+CMSIncrementalMode:使用增量模式,适合单CPU
  • XX:+UserG1GC:使用G1回收器,与G1相关的虚拟机参数都只能在jdk1.7以上使用
  • XX:+UnlockExperimentalVMOptions:允许使用实验性参数
    辅助设置:
  • -XX:+PrintGC:输出GC垃圾回收日志
  • -verbose:gc:与-XX:+PrintGC相同
  • -XX:+PrintGCDetail:输出详细的GC垃圾回收日志
  • -XX:+PrintGCTimeStamps:输出GC回收的时间戳
  • -XX:+PrintGCApplicationStoppedTIme:输出GC垃圾回收时所占用的停顿时间
  • -XX:+PrintGCApplicationConcurrentTime:输出GC并行回收时所占用的时间
  • -XX:+PrintHeapAtGC:输出GC前后详细的堆信息
  • -Xloggc:filename:把GC日志输出到filename指定的文件
  • -XX:+PrintClassHistogram:输出类信息
  • -XX:+PrintTLAB:输出TLAB空间使用情况
  • -XX:+PrintTenuringDistribution:输出每次minor GC后新的存活对象的年龄阈值

三 jdk8 jvm修改

  • 在JDK1.8中,取消了PermGen,取而代之的是Metaspace,所以PermSize和MaxPermSize参数失效,取而代之的是

-XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m

四 程序 如下:

public class TestStream {
    static List transactions=new ArrayList();
    static int len =10000000;
    public static void main(String args[]){
        System.out.println("数据量 len:"+len+" ,处理器数量:"+Runtime.getRuntime().availableProcessors()+";总内存:"+Runtime.getRuntime().totalMemory()+";最大内存:"+Runtime.getRuntime().maxMemory()+ ";free 内存:"+Runtime.getRuntime().freeMemory());
        Random rand = new Random();
        for(int i=0;i groceryTransactions = new ArrayList();
        for(Transaction t: transactions){
            if(t.getType() == Transaction.GROCERY){
                groceryTransactions.add(t);
            }
        }
        Collections.sort(groceryTransactions,new Comparator(){
            @Override
            public int compare(Object o1, Object o2) {
                Transaction t1 = (Transaction)o1;
                Transaction t2 = (Transaction)o2;
                if(t1.getValue()>t2.getValue()){
                    return 1;
                }else if(t1.getValue()==t2.getValue()){
                    return 0;
                }else{
                    return -1;
                }
                //return t1.getValue().compareTo(t2.getValue());
            }
        });
        List transactionIds = new ArrayList<>();
        for(Transaction t: groceryTransactions){
            transactionIds.add(t.getId());
            //System.out.println("id:"+t.getId());
        }
        //System.out.println("resultLen:"+ transactionIds.size());
    }
    public static void testStream(){
        List transactionsIds = transactions.stream()
                .filter(t -> t.getType() == Transaction.GROCERY)
                .sorted(comparing(Transaction::getValue).reversed())
                .map(Transaction::getId)
                .collect(toList());
        //for(int i:transactionsIds){
            //System.out.println("id:"+i);
        //}
        //System.out.println("resultLen:"+ transactionsIds.size());
    }
    public static void testParallelStream(){
        List transactionsIds = transactions.parallelStream()
                .filter(t -> t.getType() == Transaction.GROCERY)
                .sorted(comparing(Transaction::getValue).reversed())
                .map(Transaction::getId)
                .collect(toList());
        //for(int i:transactionsIds){
            //System.out.println("id:"+i );
        //}
        //System.out.println("resultLen:"+ transactionsIds.size());
    }
}
class Transaction{
    final static int GROCERY=100;
    int id;
    int type;
    String name;
    int value;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }





    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
class MyThreadQi extends Thread{

    @Override
    public void run(){
        long start=System.currentTimeMillis();
        TestStream.testJdkQie();
        System.out.println("jdk1.7time:"+(System.currentTimeMillis()-start));
    }
}
class MyThreadStream extends Thread{
    @Override
    public void run(){
        long start2=System.currentTimeMillis();
        TestStream.testStream();
        System.out.println("testStreamtime:"+(System.currentTimeMillis()-start2));
    }

}
class MyThreadParallStream extends Thread{
    @Override
    public void run(){
        long start3=System.currentTimeMillis();
        TestStream.testParallelStream();
        System.out.println("testParallelStream:"+(System.currentTimeMillis()-start3));
    }
}
class MyTransactionPhaser extends Phaser {

}

五 程序运行结果比较

运行实例一

C:\install\java\jdk1.8.0_92\bin\java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:54876,suspend=y,server=n -XX:+PrintGCDetails -Xms4G -Xmx4G -Xmn4G -XX:SurvivorRatio=2 -XX:MetaspaceSize=1G -XX:ConcGCThreads=8 -Dfile.encoding=UTF-8 -classpath C:\install\java\jdk1.8.0_92\jre\lib\charsets.jar;C:\install\java\jdk1.8.0_92\jre\lib\deploy.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\access-bridge-64.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\cldrdata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\dnsns.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jaccess.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jfxrt.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\localedata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\nashorn.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunec.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunjce_provider.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunmscapi.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunpkcs11.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\zipfs.jar;C:\install\java\jdk1.8.0_92\jre\lib\javaws.jar;C:\install\java\jdk1.8.0_92\jre\lib\jce.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfr.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfxswt.jar;C:\install\java\jdk1.8.0_92\jre\lib\jsse.jar;C:\install\java\jdk1.8.0_92\jre\lib\management-agent.jar;C:\install\java\jdk1.8.0_92\jre\lib\plugin.jar;C:\install\java\jdk1.8.0_92\jre\lib\resources.jar;C:\install\java\jdk1.8.0_92\jre\lib\rt.jar;C:\work\testHolle\out\production\testHolle;C:\work\testHolle\libs\thumbnailator-0.4.8.jar;C:\work\testHolle\libs\disruptor-3.3.4.jar;C:\install\JetBrains\IntelliJIDEA2016.2\lib\idea_rt.jar com.cby.streams.TestStream
Connected to the target VM, address: '127.0.0.1:54876', transport: 'socket'
数据量 len:10000000 ,处理器数量:8;总内存:3221749760;最大内存:3221749760;free 内存:3092869240
[GC (Allocation Failure) [PSYoungGen: 2097664K->897753K(3145728K)] 2097664K->897753K(3146240K), 0.5755101 secs] [Times: user=3.34 sys=0.27, real=0.58 secs] 
[GC (Allocation Failure) [PSYoungGen: 2995417K->792052K(3145728K)] 2995417K->792052K(3146240K), 0.4925439 secs] [Times: user=2.91 sys=0.59, real=0.49 secs] 
[GC (Allocation Failure) [PSYoungGen: 2889716K->888269K(3145728K)] 2889716K->888277K(3146240K), 0.5378810 secs] [Times: user=4.00 sys=0.02, real=0.54 secs] 
[GC (Allocation Failure) [PSYoungGen: 2985933K->851222K(3145728K)] 2985941K->851238K(3146240K), 0.5981777 secs] [Times: user=4.30 sys=0.08, real=0.60 secs] 
[GC (Allocation Failure) [PSYoungGen: 2915628K->752462K(3145728K)] 2915644K->752486K(3146240K), 0.7721996 secs] [Times: user=5.52 sys=0.00, real=0.77 secs] 
testParallelStream:7784
[GC (Allocation Failure) [PSYoungGen: 2850126K->509184K(3128320K)] 2850150K->509216K(3128832K), 0.5488668 secs] [Times: user=3.70 sys=0.00, real=0.55 secs] 
[GC (Allocation Failure) --[PSYoungGen: 2675091K->2675091K(3128320K)] 2675123K->2675595K(3128832K), 3.9532986 secs] [Times: user=14.77 sys=6.25, real=3.95 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2675091K->612067K(3128320K)] [ParOldGen: 504K->493K(512K)] 2675595K->612561K(3128832K), [Metaspace: 5186K->5154K(1056768K)], 6.3401446 secs] [Times: user=10.26 sys=0.05, real=6.34 secs] 
jdk1.7time:23448
Disconnected from the target VM, address: '127.0.0.1:54876', transport: 'socket'
Java HotSpot(TM) 64-Bit Server VM warning: MaxNewSize (4194304k) is equal to or greater than the entire heap (4194304k).  A new max generation size of 4193792k will be used.
testStreamtime:25679
Heap
 PSYoungGen      total 3128320K, used 2610370K [0x00000006c0080000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2188800K, 91% used [0x00000006c0080000,0x0000000739ff78e8,0x0000000745a00000)
  from space 939520K, 65% used [0x0000000786a80000,0x00000007ac038fe8,0x00000007c0000000)
  to   space 1002496K, 0% used [0x0000000745a00000,0x0000000745a00000,0x0000000782d00000)
 ParOldGen       total 512K, used 493K [0x00000006c0000000, 0x00000006c0080000, 0x00000006c0080000)
  object space 512K, 96% used [0x00000006c0000000,0x00000006c007b728,0x00000006c0080000)
 Metaspace       used 5169K, capacity 5450K, committed 5632K, reserved 1056768K
  class space    used 595K, capacity 659K, committed 768K, reserved 1048576K

Process finished with exit code 0

运行实例二

C:\install\java\jdk1.8.0_92\bin\java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:54970,suspend=y,server=n -XX:+PrintGCDetails -Xms4G -Xmx4G -Xmn4G -XX:SurvivorRatio=2 -XX:MetaspaceSize=1G -XX:ConcGCThreads=8 -Dfile.encoding=UTF-8 -classpath C:\install\java\jdk1.8.0_92\jre\lib\charsets.jar;C:\install\java\jdk1.8.0_92\jre\lib\deploy.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\access-bridge-64.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\cldrdata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\dnsns.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jaccess.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jfxrt.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\localedata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\nashorn.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunec.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunjce_provider.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunmscapi.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunpkcs11.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\zipfs.jar;C:\install\java\jdk1.8.0_92\jre\lib\javaws.jar;C:\install\java\jdk1.8.0_92\jre\lib\jce.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfr.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfxswt.jar;C:\install\java\jdk1.8.0_92\jre\lib\jsse.jar;C:\install\java\jdk1.8.0_92\jre\lib\management-agent.jar;C:\install\java\jdk1.8.0_92\jre\lib\plugin.jar;C:\install\java\jdk1.8.0_92\jre\lib\resources.jar;C:\install\java\jdk1.8.0_92\jre\lib\rt.jar;C:\work\testHolle\out\production\testHolle;C:\work\testHolle\libs\thumbnailator-0.4.8.jar;C:\work\testHolle\libs\disruptor-3.3.4.jar;C:\install\JetBrains\IntelliJIDEA2016.2\lib\idea_rt.jar com.cby.streams.TestStream
Connected to the target VM, address: '127.0.0.1:54970', transport: 'socket'
数据量 len:10000000 ,处理器数量:8;总内存:3221749760;最大内存:3221749760;free 内存:3092869240
[GC (Allocation Failure) [PSYoungGen: 2097664K->825531K(3145728K)] 2097664K->825539K(3146240K), 0.5856998 secs] [Times: user=2.67 sys=0.41, real=0.59 secs] 
[GC (Allocation Failure) [PSYoungGen: 2923195K->870193K(3145728K)] 2923203K->870201K(3146240K), 0.5162155 secs] [Times: user=3.17 sys=0.48, real=0.52 secs] 
[GC (Allocation Failure) [PSYoungGen: 2967857K->1029451K(3145728K)] 2967865K->1029459K(3146240K), 0.5695083 secs] [Times: user=3.91 sys=0.13, real=0.57 secs] 
[GC (Allocation Failure) [PSYoungGen: 3127115K->851270K(3145728K)] 3127123K->851278K(3146240K), 0.6019488 secs] [Times: user=4.19 sys=0.00, real=0.60 secs] 
testParallelStream:6677
[GC (Allocation Failure) [PSYoungGen: 2948934K->542834K(3145728K)] 2948942K->542850K(3146240K), 0.4214606 secs] [Times: user=3.08 sys=0.00, real=0.42 secs] 
[GC (Allocation Failure) [PSYoungGen: 2640498K->509248K(3145728K)] 2640514K->509264K(3146240K), 0.4577998 secs] [Times: user=3.17 sys=0.00, real=0.46 secs] 
[GC (Allocation Failure) --[PSYoungGen: 2606912K->2606912K(3145728K)] 2606928K->2607416K(3146240K), 4.3252101 secs] [Times: user=13.98 sys=7.73, real=4.33 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2606912K->628401K(3145728K)] [ParOldGen: 504K->483K(512K)] 2607416K->628884K(3146240K), [Metaspace: 5185K->5153K(1056768K)], 6.3848603 secs] [Times: user=10.28 sys=0.09, real=6.38 secs] 
jdk1.7time:23353
testStreamtime:25742
Heap
 PSYoungGen      total 3145728K, used 2610978K [0x00000006c0080000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2097664K, 94% used [0x00000006c0080000,0x000000073909c4e8,0x0000000740100000)
  from space 1048064K, 59% used [0x0000000780080000,0x00000007a662c490,0x00000007c0000000)
  to   space 1048064K, 0% used [0x0000000740100000,0x0000000740100000,0x0000000780080000)
 ParOldGen       total 512K, used 483K [0x00000006c0000000, 0x00000006c0080000, 0x00000006c0080000)
  object space 512K, 94% used [0x00000006c0000000,0x00000006c0078c40,0x00000006c0080000)
 Metaspace       used 5167K, capacity 5450K, committed 5632K, reserved 1056768K
  class space    used 595K, capacity 659K, committed 768K, reserved 1048576K
Disconnected from the target VM, address: '127.0.0.1:54970', transport: 'socket'
Java HotSpot(TM) 64-Bit Server VM warning: MaxNewSize (4194304k) is equal to or greater than the entire heap (4194304k).  A new max generation size of 4193792k will be used.

Process finished with exit code 0

运行实例三


C:\install\java\jdk1.8.0_92\bin\java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:54988,suspend=y,server=n -XX:+PrintGCDetails -Xms4G -Xmx4G -Xmn4G -XX:SurvivorRatio=3 -XX:MetaspaceSize=1G -XX:ConcGCThreads=8 -Dfile.encoding=UTF-8 -classpath C:\install\java\jdk1.8.0_92\jre\lib\charsets.jar;C:\install\java\jdk1.8.0_92\jre\lib\deploy.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\access-bridge-64.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\cldrdata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\dnsns.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jaccess.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jfxrt.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\localedata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\nashorn.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunec.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunjce_provider.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunmscapi.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunpkcs11.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\zipfs.jar;C:\install\java\jdk1.8.0_92\jre\lib\javaws.jar;C:\install\java\jdk1.8.0_92\jre\lib\jce.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfr.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfxswt.jar;C:\install\java\jdk1.8.0_92\jre\lib\jsse.jar;C:\install\java\jdk1.8.0_92\jre\lib\management-agent.jar;C:\install\java\jdk1.8.0_92\jre\lib\plugin.jar;C:\install\java\jdk1.8.0_92\jre\lib\resources.jar;C:\install\java\jdk1.8.0_92\jre\lib\rt.jar;C:\work\testHolle\out\production\testHolle;C:\work\testHolle\libs\thumbnailator-0.4.8.jar;C:\work\testHolle\libs\disruptor-3.3.4.jar;C:\install\JetBrains\IntelliJIDEA2016.2\lib\idea_rt.jar com.cby.streams.TestStream
Connected to the target VM, address: '127.0.0.1:54988', transport: 'socket'
数据量 len:10000000 ,处理器数量:8;总内存:3436183552;最大内存:3436183552;free 内存:3281570976
[GC (Allocation Failure) --[PSYoungGen: 2516480K->2516480K(3355136K)] 2516480K->2516992K(3355648K), 1.3077216 secs] [Times: user=4.06 sys=0.52, real=1.31 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2516480K->554938K(3355136K)] [ParOldGen: 512K->510K(512K)] 2516992K->555448K(3355648K), [Metaspace: 5125K->5094K(1056768K)], 4.9296281 secs] [Times: user=10.39 sys=0.09, real=4.93 secs] 
[GC (Allocation Failure) --[PSYoungGen: 2516480K->2516480K(3355136K)] 2516990K->2516990K(3355648K), 2.7672171 secs] [Times: user=5.81 sys=8.59, real=2.77 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2516480K->556724K(3355136K)] [ParOldGen: 510K->510K(512K)] 2516990K->557234K(3355648K), [Metaspace: 5094K->5094K(1056768K)], 3.2280258 secs] [Times: user=7.34 sys=0.00, real=3.23 secs] 
[GC (Allocation Failure) --[PSYoungGen: 2516480K->2516480K(3355136K)] 2516990K->2516990K(3355648K), 1.3057888 secs] [Times: user=4.50 sys=0.02, real=1.31 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2516480K->559795K(3355136K)] [ParOldGen: 510K->510K(512K)] 2516990K->560305K(3355648K), [Metaspace: 5095K->5095K(1056768K)], 7.1458706 secs] [Times: user=11.34 sys=0.00, real=7.15 secs] 
[GC (Allocation Failure) --[PSYoungGen: 2516480K->2516480K(3355136K)] 2516990K->2516990K(3355648K), 2.2252787 secs] [Times: user=5.55 sys=5.38, real=2.23 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2516480K->561839K(3355136K)] [ParOldGen: 510K->511K(512K)] 2516990K->562351K(3355648K), [Metaspace: 5095K->5095K(1056768K)], 5.5406485 secs] [Times: user=9.39 sys=0.01, real=5.54 secs] 
[GC (Allocation Failure) --[PSYoungGen: 2516480K->2516480K(3355136K)] 2516991K->2516991K(3355648K), 2.0193980 secs] [Times: user=6.31 sys=3.45, real=2.02 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2516480K->733602K(3355136K)] [ParOldGen: 511K->511K(512K)] 2516991K->734113K(3355648K), [Metaspace: 5147K->5147K(1056768K)], 9.3020061 secs] [Times: user=15.17 sys=0.03, real=9.30 secs] 
testParallelStream:44316
[GC (Allocation Failure) --[PSYoungGen: 2516480K->2516480K(3355136K)] 2516991K->2516991K(3355648K), 1.1816019 secs] [Times: user=4.13 sys=0.00, real=1.18 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2516480K->500062K(3355136K)] [ParOldGen: 511K->511K(512K)] 2516991K->500574K(3355648K), [Metaspace: 5147K->5147K(1056768K)], 4.9312130 secs] [Times: user=7.83 sys=0.03, real=4.93 secs] 
[GC (Allocation Failure) --[PSYoungGen: 2516480K->2516480K(3355136K)] 2516991K->2516991K(3355648K), 1.2330462 secs] [Times: user=4.23 sys=0.00, real=1.23 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2516480K->508221K(3355136K)] [ParOldGen: 511K->511K(512K)] 2516991K->508733K(3355648K), [Metaspace: 5147K->5147K(1056768K)], 4.9728002 secs] [Times: user=8.31 sys=0.00, real=4.97 secs] 
jdk1.7time:61649
[GC (Allocation Failure) --[PSYoungGen: 2516480K->2516480K(3355136K)] 2516991K->2516991K(3355648K), 2.0911757 secs] [Times: user=7.36 sys=2.95, real=2.09 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2516480K->437721K(3355136K)] [ParOldGen: 511K->511K(512K)] 2516991K->438233K(3355648K), [Metaspace: 5148K->5148K(1056768K)], 4.0821329 secs] [Times: user=9.16 sys=0.08, real=4.08 secs] 
testStreamtime:69848
Heap
 PSYoungGen      total 3355136K, used 1141650K [0x00000006c0080000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2516480K, 45% used [0x00000006c0080000,0x0000000705b64990,0x0000000759a00000)
  from space 838656K, 0% used [0x000000078cd00000,0x000000078cd00000,0x00000007c0000000)
  to   space 838656K, 0% used [0x0000000759a00000,0x0000000759a00000,0x000000078cd00000)
 ParOldGen       total 512K, used 511K [0x00000006c0000000, 0x00000006c0080000, 0x00000006c0080000)
Disconnected from the target VM, address: '127.0.0.1:54988', transport: 'socket'
  object space 512K, 99% used [0x00000006c0000000,0x00000006c007ff00,0x00000006c0080000)
 Metaspace       used 5160K, capacity 5450K, committed 5632K, reserved 1056768K
  class space    used 595K, capacity 659K, committed 768K, reserved 1048576K
Java HotSpot(TM) 64-Bit Server VM warning: MaxNewSize (4194304k) is equal to or greater than the entire heap (4194304k).  A new max generation size of 4193792k will be used.

Process finished with exit code 0

运行实例四


C:\install\java\jdk1.8.0_92\bin\java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:55178,suspend=y,server=n -XX:+PrintGCDetails -Xms4G -Xmx4G -Xmn4G -XX:SurvivorRatio=2 -XX:MetaspaceSize=1G -XX:ConcGCThreads=8 -Dfile.encoding=UTF-8 -classpath C:\install\java\jdk1.8.0_92\jre\lib\charsets.jar;C:\install\java\jdk1.8.0_92\jre\lib\deploy.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\access-bridge-64.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\cldrdata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\dnsns.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jaccess.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jfxrt.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\localedata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\nashorn.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunec.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunjce_provider.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunmscapi.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunpkcs11.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\zipfs.jar;C:\install\java\jdk1.8.0_92\jre\lib\javaws.jar;C:\install\java\jdk1.8.0_92\jre\lib\jce.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfr.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfxswt.jar;C:\install\java\jdk1.8.0_92\jre\lib\jsse.jar;C:\install\java\jdk1.8.0_92\jre\lib\management-agent.jar;C:\install\java\jdk1.8.0_92\jre\lib\plugin.jar;C:\install\java\jdk1.8.0_92\jre\lib\resources.jar;C:\install\java\jdk1.8.0_92\jre\lib\rt.jar;C:\work\testHolle\out\production\testHolle;C:\work\testHolle\libs\thumbnailator-0.4.8.jar;C:\work\testHolle\libs\disruptor-3.3.4.jar;C:\install\JetBrains\IntelliJIDEA2016.2\lib\idea_rt.jar com.cby.streams.TestStream
Connected to the target VM, address: '127.0.0.1:55178', transport: 'socket'
数据量 len:10000000 ,处理器数量:8;总内存:3221749760;最大内存:3221749760;free 内存:3092869240
[GC (Allocation Failure) [PSYoungGen: 2097664K->897817K(3145728K)] 2097664K->897825K(3146240K), 0.5095752 secs] [Times: user=2.88 sys=0.67, real=0.51 secs] 
[GC (Allocation Failure) [PSYoungGen: 2995481K->900265K(3145728K)] 2995489K->900281K(3146240K), 0.5364439 secs] [Times: user=2.95 sys=0.73, real=0.54 secs] 
[GC (Allocation Failure) [PSYoungGen: 2997929K->1020509K(3145728K)] 2997945K->1020525K(3146240K), 0.5817602 secs] [Times: user=4.13 sys=0.06, real=0.58 secs] 
[GC (Allocation Failure) [PSYoungGen: 3118173K->812224K(3145728K)] 3118189K->812248K(3146240K), 0.8520321 secs] [Times: user=3.91 sys=0.00, real=0.85 secs] 
testParallelStream:7011
[GC (Allocation Failure) [PSYoungGen: 2909888K->609205K(3145728K)] 2909912K->609237K(3146240K), 0.4780591 secs] [Times: user=2.98 sys=0.00, real=0.48 secs] 
[GC (Allocation Failure) [PSYoungGen: 2706869K->579682K(3145728K)] 2706901K->579722K(3146240K), 0.4263651 secs] [Times: user=3.30 sys=0.00, real=0.43 secs] 
[GC (Allocation Failure) --[PSYoungGen: 2647180K->2647180K(3145728K)] 2647220K->2647684K(3146240K), 3.9023984 secs] [Times: user=13.89 sys=5.73, real=3.90 secs] 
[Full GC (Ergonomics) [PSYoungGen: 2647180K->612104K(3145728K)] [ParOldGen: 504K->460K(512K)] 2647684K->612564K(3146240K), [Metaspace: 5182K->5150K(1056768K)], 6.6610483 secs] [Times: user=11.56 sys=0.06, real=6.66 secs] 
jdk1.7time:23695
Disconnected from the target VM, address: '127.0.0.1:55178', transport: 'socket'
testStreamtime:26177
Heap
 PSYoungGen      total 3145728K, used 2674152K [0x00000006c0080000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2097664K, 98% used [0x00000006c0080000,0x000000073de37ed8,0x0000000740100000)
  from space 1048064K, 58% used [0x0000000780080000,0x00000007a5642288,0x00000007c0000000)
  to   space 1048064K, 0% used [0x0000000740100000,0x0000000740100000,0x0000000780080000)
 ParOldGen       total 512K, used 460K [0x00000006c0000000, 0x00000006c0080000, 0x00000006c0080000)
  object space 512K, 89% used [0x00000006c0000000,0x00000006c0073038,0x00000006c0080000)
 Metaspace       used 5165K, capacity 5450K, committed 5632K, reserved 1056768K
  class space    used 595K, capacity 659K, committed 768K, reserved 1048576K
Java HotSpot(TM) 64-Bit Server VM warning: MaxNewSize (4194304k) is equal to or greater than the entire heap (4194304k).  A new max generation size of 4193792k will be used.

Process finished with exit code 0

运行实例五

C:\install\java\jdk1.8.0_92\bin\java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:55223,suspend=y,server=n -XX:+PrintGCDetails -Xms5G -Xmx5G -Xmn5G -XX:SurvivorRatio=2 -XX:MetaspaceSize=1G -XX:ConcGCThreads=8 -Dfile.encoding=UTF-8 -classpath C:\install\java\jdk1.8.0_92\jre\lib\charsets.jar;C:\install\java\jdk1.8.0_92\jre\lib\deploy.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\access-bridge-64.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\cldrdata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\dnsns.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jaccess.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jfxrt.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\localedata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\nashorn.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunec.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunjce_provider.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunmscapi.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunpkcs11.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\zipfs.jar;C:\install\java\jdk1.8.0_92\jre\lib\javaws.jar;C:\install\java\jdk1.8.0_92\jre\lib\jce.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfr.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfxswt.jar;C:\install\java\jdk1.8.0_92\jre\lib\jsse.jar;C:\install\java\jdk1.8.0_92\jre\lib\management-agent.jar;C:\install\java\jdk1.8.0_92\jre\lib\plugin.jar;C:\install\java\jdk1.8.0_92\jre\lib\resources.jar;C:\install\java\jdk1.8.0_92\jre\lib\rt.jar;C:\work\testHolle\out\production\testHolle;C:\work\testHolle\libs\thumbnailator-0.4.8.jar;C:\work\testHolle\libs\disruptor-3.3.4.jar;C:\install\JetBrains\IntelliJIDEA2016.2\lib\idea_rt.jar com.cby.streams.TestStream
Connected to the target VM, address: '127.0.0.1:55223', transport: 'socket'
数据量 len:10000000 ,处理器数量:8;总内存:4027056128;最大内存:4027056128;free 内存:3865963336
[GC (Allocation Failure) [PSYoungGen: 2621952K->898729K(3932160K)] 2621952K->898729K(3932672K), 0.5855827 secs] [Times: user=3.28 sys=0.55, real=0.59 secs] 
[GC (Allocation Failure) [PSYoungGen: 3520681K->901273K(3932160K)] 3520681K->901273K(3932672K), 0.5526856 secs] [Times: user=3.19 sys=0.59, real=0.55 secs] 
[GC (Allocation Failure) [PSYoungGen: 3523225K->983510K(3932160K)] 3523225K->983518K(3932672K), 0.5946495 secs] [Times: user=4.02 sys=0.11, real=0.60 secs] 
[GC (Allocation Failure) [PSYoungGen: 3605462K->801480K(3932160K)] 3605470K->801496K(3932672K), 0.6351478 secs] [Times: user=4.36 sys=0.00, real=0.63 secs] 
testParallelStream:7007
[GC (Allocation Failure) [PSYoungGen: 3423432K->650117K(3932160K)] 3423448K->650141K(3932672K), 0.4838129 secs] [Times: user=3.75 sys=0.00, real=0.48 secs] 
jdk1.7time:12922
[GC (Allocation Failure) [PSYoungGen: 3272069K->455069K(4062720K)] 3272093K->455101K(4063232K), 0.3747322 secs] [Times: user=2.91 sys=0.00, real=0.37 secs] 
Disconnected from the target VM, address: '127.0.0.1:55223', transport: 'socket'
testStreamtime:15245
Heap
 PSYoungGen      total 4062720K, used 1559107K [0x0000000680080000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 3006976K, 36% used [0x0000000680080000,0x00000006c36a98d0,0x0000000737900000)
  from space 1055744K, 43% used [0x000000077f900000,0x000000079b5675c0,0x00000007c0000000)
  to   space 1117696K, 0% used [0x0000000737900000,0x0000000737900000,0x000000077bc80000)
 ParOldGen       total 512K, used 32K [0x0000000680000000, 0x0000000680080000, 0x0000000680080000)
  object space 512K, 6% used [0x0000000680000000,0x0000000680008000,0x0000000680080000)
 Metaspace       used 5200K, capacity 5506K, committed 5632K, reserved 1056768K
  class space    used 608K, capacity 683K, committed 768K, reserved 1048576K
Java HotSpot(TM) 64-Bit Server VM warning: MaxNewSize (5242880k) is equal to or greater than the entire heap (5242880k).  A new max generation size of 5242368k will be used.

Process finished with exit code 0

运行实例六

C:\install\java\jdk1.8.0_92\bin\java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:55510,suspend=y,server=n -XX:+PrintGCDetails -Xms5G -Xmx5G -Xmn5G -XX:SurvivorRatio=2 -XX:MetaspaceSize=1G -XX:ConcGCThreads=8 -XX:NewRatio=2 -Dfile.encoding=UTF-8 -classpath C:\install\java\jdk1.8.0_92\jre\lib\charsets.jar;C:\install\java\jdk1.8.0_92\jre\lib\deploy.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\access-bridge-64.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\cldrdata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\dnsns.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jaccess.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\jfxrt.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\localedata.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\nashorn.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunec.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunjce_provider.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunmscapi.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\sunpkcs11.jar;C:\install\java\jdk1.8.0_92\jre\lib\ext\zipfs.jar;C:\install\java\jdk1.8.0_92\jre\lib\javaws.jar;C:\install\java\jdk1.8.0_92\jre\lib\jce.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfr.jar;C:\install\java\jdk1.8.0_92\jre\lib\jfxswt.jar;C:\install\java\jdk1.8.0_92\jre\lib\jsse.jar;C:\install\java\jdk1.8.0_92\jre\lib\management-agent.jar;C:\install\java\jdk1.8.0_92\jre\lib\plugin.jar;C:\install\java\jdk1.8.0_92\jre\lib\resources.jar;C:\install\java\jdk1.8.0_92\jre\lib\rt.jar;C:\work\testHolle\out\production\testHolle;C:\work\testHolle\libs\thumbnailator-0.4.8.jar;C:\work\testHolle\libs\disruptor-3.3.4.jar;C:\install\JetBrains\IntelliJIDEA2016.2\lib\idea_rt.jar com.cby.streams.TestStream
Connected to the target VM, address: '127.0.0.1:55510', transport: 'socket'
数据量 len:10000000 ,处理器数量:8;总内存:4027056128;最大内存:4027056128;free 内存:3865963336
[GC (Allocation Failure) [PSYoungGen: 2621952K->961546K(3932160K)] 2621952K->961554K(3932672K), 0.5499488 secs] [Times: user=2.88 sys=0.70, real=0.55 secs] 
[GC (Allocation Failure) [PSYoungGen: 3583498K->927363K(3932160K)] 3583506K->927371K(3932672K), 0.6221797 secs] [Times: user=3.27 sys=0.72, real=0.62 secs] 
[GC (Allocation Failure) [PSYoungGen: 3549315K->888269K(3932160K)] 3549323K->888285K(3932672K), 0.6171502 secs] [Times: user=4.13 sys=0.02, real=0.62 secs] 
testParallelStream:6599
[GC (Allocation Failure) [PSYoungGen: 3510221K->609205K(3932160K)] 3510237K->609229K(3932672K), 0.4739420 secs] [Times: user=3.24 sys=0.00, real=0.48 secs] 
[GC (Allocation Failure) [PSYoungGen: 3231157K->509200K(3932160K)] 3231181K->509224K(3932672K), 0.4505993 secs] [Times: user=3.55 sys=0.00, real=0.45 secs] 
jdk1.7time:12399
[GC (Allocation Failure) [PSYoungGen: 3131152K->438701K(4068352K)] 3131176K->438733K(4068864K), 0.3719724 secs] [Times: user=2.91 sys=0.02, real=0.37 secs] 
Disconnected from the target VM, address: '127.0.0.1:55510', transport: 'socket'
testStreamtime:15484
Heap
 PSYoungGen      total 4068352K, used 1584370K [0x0000000680080000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 2951680K, 38% used [0x0000000680080000,0x00000006c5f513c0,0x0000000734300000)
  from space 1116672K, 39% used [0x000000077bd80000,0x00000007969eb5a0,0x00000007c0000000)
  to   space 1145344K, 0% used [0x0000000734300000,0x0000000734300000,0x000000077a180000)
 ParOldGen       total 512K, used 32K [0x0000000680000000, 0x0000000680080000, 0x0000000680080000)
  object space 512K, 6% used [0x0000000680000000,0x0000000680008000,0x0000000680080000)
 Metaspace       used 5207K, capacity 5506K, committed 5632K, reserved 1056768K
  class space    used 608K, capacity 683K, committed 768K, reserved 1048576K
Java HotSpot(TM) 64-Bit Server VM warning: MaxNewSize (5242880k) is equal to or greater than the entire heap (5242880k).  A new max generation size of 5242368k will be used.

Process finished with exit code 0

运行实例七

 

六 总结 

1  1千万记录3个线程,最短运行实际是6.599秒,最长运行时间69.848秒。

2  该程序有 从jdk1.7 传统处理,聚合操作到jdk1.8 stream的串行,再到并行处理,性能进行分析。

3  该程序从 jvm参数优化方面进行了性能分析。

参考

https://blog.csdn.net/miracle_8/article/details/78347172

https://blog.csdn.net/u013980127/article/details/53913994

JDK8中JVM堆内存划分

JDK8引进的JVM参数变化记录

JVM 不稳定参数

java 8 JVM性能优化

Java性能优化攻略详解

Jvm垃圾回收器详细

JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解

jps命令(Java Virtual Machine Process Status Tool)

Tomcat 8 安装和配置、优化

http://www.jvmmonitor.org/doc/index.html

你可能感兴趣的:(性能优化,java)