调优案例与实战(笔记)——eclipse部分

一、编译时间和类加载时间的优化

eclipse的使用者众多,它的编译代码我们可以认为是可靠的,不需要在加载的时候进行字节码验证,因此通过参数-Xverify:none禁止掉字节码验证过程可以作为一项优化措施。(首次添加这个参数后,类加载的时间从44.20ms降低到了23.68ms)。

后续在进行其它优化后把这个参数去掉,发现类加载的时间好像没怎么受影响了。。。

二、编译时间

1、定义:虚拟机的JIT编译器(Just In Time Compiler)编译热点代码(Hot Spot Code)的耗时。

2、原因:Java语言为了实现跨平台的特性,Java代码编译出来后形成的Class文件存储的是字节码(ByteCode),虚拟机通过解释方式执行字节码命令,比起C/C++编译成本地二进制码来说,速度要慢了不少。——解决方案:虚拟机内置了两个运行时编译器。如果一段Java方法被调用次数达到一定程度,就会被判定为热代码交给JIT编译器即时编译为本地代码,提高运行速度(HotSpot虚拟机名字的由来)。——优点:A、运行期可以收集很多编译器不知道的信息,所以它在运行期编译出来的代码甚至很可能比C/C++的编译器静态编译出来的更优秀;B、Java程序只要代码没有问题(主要是泄漏问题),随着代码被编译得越来越彻底,运行速度应当是越来越快的。——缺点:编译需要消耗程序正常的运行时间,也就是常说的“编译时间”。

3、参数——-Xint可以禁止编译器运作(使用后效果更差)

三、调整内存设置控制垃圾收集频率

在绝大多数应用中,不可能出现持续不断的类被加载和卸载。在程序运行一段时间后,热点方法不断被编译,新的热点方法数量也总会下降,但是垃圾收集则是随着程序运行而不断运作的,所以它对性能的影响才是最重要的。

下面就是eclipse启动的过程中GC的具体情况

Java HotSpot(TM) 64-Bit Server VM (25.91-b14) for windows-amd64 JRE (1.8.0_91-b14), built on Apr  1 2016 00:58:32 by "java_re" with MS VC++ 10.0 (VS2010)
Memory: 4k page, physical 4080628k(848424k free), swap 8159380k(4694192k free)
CommandLine flags: -XX:-BytecodeVerificationLocal -XX:-BytecodeVerificationRemote -XX:InitialHeapSize=41943040 -XX:MaxHeapSize=805306368 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC 
0.362: [GC (Allocation Failure) [PSYoungGen: 10240K->1528K(11776K)] 10240K->2365K(39424K), 0.0043803 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
0.592: [GC (Allocation Failure) [PSYoungGen: 11762K->1536K(11776K)] 12599K->5268K(39424K), 0.0046758 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
0.831: [GC (Allocation Failure) [PSYoungGen: 11776K->1533K(11776K)] 15508K->8389K(39424K), 0.0060921 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
1.277: [GC (Allocation Failure) [PSYoungGen: 11773K->1534K(22016K)] 18629K->12294K(49664K), 0.0075822 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
1.801: [GC (Allocation Failure) [PSYoungGen: 22014K->1520K(22016K)] 32774K->22728K(49664K), 0.0130537 secs] [Times: user=0.05 sys=0.01, real=0.01 secs] 
1.814: [Full GC (Ergonomics) [PSYoungGen: 1520K->0K(22016K)] [ParOldGen: 21208K->21785K(50176K)] 22728K->21785K(72192K), [Metaspace: 19020K->19020K(1069056K)], 0.1182043 secs] [Times: user=0.45 sys=0.00, real=0.12 secs] 
2.326: [GC (Allocation Failure) [PSYoungGen: 20480K->1548K(27136K)] 42265K->23342K(77312K), 0.0064484 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
2.474: [GC (Allocation Failure) [PSYoungGen: 26636K->1152K(32256K)] 48430K->22953K(82432K), 0.0024647 secs] [Times: user=0.05 sys=0.00, real=0.00 secs] 
2.646: [GC (Allocation Failure) [PSYoungGen: 31872K->1312K(43008K)] 53673K->23121K(93184K), 0.0030417 secs] [Times: user=0.03 sys=0.02, real=0.00 secs] 
2.780: [GC (Allocation Failure) [PSYoungGen: 32544K->1424K(42496K)] 54353K->23233K(92672K), 0.0023269 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
3.305: [GC (Allocation Failure) [PSYoungGen: 32656K->8587K(45568K)] 54465K->30405K(95744K), 0.0105927 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
4.127: [GC (Allocation Failure) [PSYoungGen: 42891K->12260K(46592K)] 64709K->40327K(96768K), 0.0275842 secs] [Times: user=0.06 sys=0.00, real=0.03 secs] 
4.441: [GC (Metadata GC Threshold) [PSYoungGen: 25785K->14774K(54784K)] 53852K->42849K(104960K), 0.0374794 secs] [Times: user=0.06 sys=0.00, real=0.04 secs] 
4.479: [Full GC (Metadata GC Threshold) [PSYoungGen: 14774K->0K(54784K)] [ParOldGen: 28075K->29381K(68096K)] 42849K->29381K(122880K), [Metaspace: 32316K->32316K(1079296K)], 0.1281804 secs] [Times: user=0.33 sys=0.00, real=0.13 secs] 
5.398: [GC (Allocation Failure) [PSYoungGen: 37888K->10566K(56832K)] 67269K->39956K(124928K), 0.0097060 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
6.184: [GC (Allocation Failure) [PSYoungGen: 48454K->14632K(65024K)] 77844K->44030K(133120K), 0.0237384 secs] [Times: user=0.11 sys=0.00, real=0.02 secs] 
7.218: [GC (Allocation Failure) [PSYoungGen: 60200K->12851K(66560K)] 89598K->42257K(134656K), 0.0149979 secs] [Times: user=0.05 sys=0.00, real=0.01 secs] 
7.980: [GC (Metadata GC Threshold) [PSYoungGen: 42782K->15466K(75264K)] 72187K->44880K(143360K), 0.0244297 secs] [Times: user=0.09 sys=0.00, real=0.03 secs] 
8.004: [Full GC (Metadata GC Threshold) [PSYoungGen: 15466K->0K(75264K)] [ParOldGen: 29413K->42629K(92160K)] 44880K->42629K(167424K), [Metaspace: 54746K->54726K(1101824K)], 0.3295972 secs] [Times: user=0.73 sys=0.00, real=0.33 secs] 
9.167: [GC (Allocation Failure) [PSYoungGen: 53760K->13504K(76800K)] 96389K->56141K(168960K), 0.0142248 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 
9.378: [GC (Allocation Failure) [PSYoungGen: 67264K->11860K(83456K)] 109901K->54505K(175616K), 0.0250811 secs] [Times: user=0.06 sys=0.03, real=0.02 secs] 
10.541: [GC (Allocation Failure) [PSYoungGen: 72276K->17420K(82944K)] 114921K->60074K(175104K), 0.0437416 secs] [Times: user=0.06 sys=0.02, real=0.04 secs] 
15.329: [GC (Allocation Failure) [PSYoungGen: 77836K->22218K(91648K)] 120490K->64880K(183808K), 0.0276132 secs] [Times: user=0.13 sys=0.00, real=0.03 secs] 
15.858: [GC (GCLocker Initiated GC) [PSYoungGen: 89802K->27122K(94720K)] 132465K->74341K(186880K), 0.0346877 secs] [Times: user=0.11 sys=0.03, real=0.04 secs] 
16.319: [GC (System.gc()) [PSYoungGen: 93084K->26632K(109056K)] 140302K->73858K(201216K), 0.0250433 secs] [Times: user=0.03 sys=0.03, real=0.02 secs] 
16.344: [Full GC (System.gc()) [PSYoungGen: 26632K->0K(109056K)] [ParOldGen: 47226K->62259K(92160K)] 73858K->62259K(201216K), [Metaspace: 75954K->75954K(1120256K)], 0.4645618 secs] [Times: user=1.19 sys=0.09, real=0.46 secs] 
18.547: [GC (Allocation Failure) [PSYoungGen: 75776K->13566K(111616K)] 138039K->75838K(203776K), 0.0208283 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 
19.320: [GC (Allocation Failure) [PSYoungGen: 89342K->24464K(119296K)] 151614K->86743K(211456K), 0.0272631 secs] [Times: user=0.11 sys=0.02, real=0.03 secs] 
20.207: [GC (Allocation Failure) [PSYoungGen: 109456K->35824K(120832K)] 171735K->99044K(212992K), 0.0310830 secs] [Times: user=0.13 sys=0.00, real=0.03 secs] 

一共进行了4次FULL GC、22次Minor GC。

经过这些GC后,新生代的内存被扩展到了120832K(可以设置初始为128M),老年代被扩展到了92160K(可以设置初始为96M)。——通过扩大新生代和老年代的初始内存大小来减少GC次数。

具体可通过下面的参数设置虚拟机初始新生代、老年代和堆的大小。

-Xmx512m
-Xms512m
-Xmn128m
-XX:PerSize=96m
-XX:MaxPermSize=96m

开启eclipse后,日志内容如下。

Java HotSpot(TM) 64-Bit Server VM (25.91-b14) for windows-amd64 JRE (1.8.0_91-b14), built on Apr  1 2016 00:58:32 by "java_re" with MS VC++ 10.0 (VS2010)
Memory: 4k page, physical 4080628k(1204940k free), swap 8159380k(4297800k free)
CommandLine flags: -XX:-BytecodeVerificationLocal -XX:-BytecodeVerificationRemote -XX:InitialHeapSize=536870912 -XX:MaxHeapSize=536870912 -XX:MaxNewSize=134217728 -XX:NewSize=134217728 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC 
5.122: [GC (Metadata GC Threshold) [PSYoungGen: 72800K->16367K(114688K)] 72800K->22823K(507904K), 0.0245345 secs] [Times: user=0.06 sys=0.00, real=0.03 secs] 
5.146: [Full GC (Metadata GC Threshold) [PSYoungGen: 16367K->0K(114688K)] [ParOldGen: 6456K->22385K(393216K)] 22823K->22385K(507904K), [Metaspace: 19051K->19051K(1069056K)], 0.0595407 secs] [Times: user=0.20 sys=0.00, real=0.06 secs] 
5.727: [GC (Allocation Failure) [PSYoungGen: 98304K->2149K(114688K)] 120689K->24543K(507904K), 0.0045317 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
10.994: [GC (Allocation Failure) [PSYoungGen: 100453K->16373K(114688K)] 122847K->45699K(507904K), 0.0222810 secs] [Times: user=0.06 sys=0.01, real=0.02 secs] 
11.107: [GC (Metadata GC Threshold) [PSYoungGen: 18782K->14917K(114688K)] 48108K->44250K(507904K), 0.0387124 secs] [Times: user=0.17 sys=0.02, real=0.04 secs] 
11.145: [Full GC (Metadata GC Threshold) [PSYoungGen: 14917K->0K(114688K)] [ParOldGen: 29333K->28875K(393216K)] 44250K->28875K(507904K), [Metaspace: 32349K->32349K(1081344K)], 0.1143577 secs] [Times: user=0.34 sys=0.00, real=0.11 secs] 
17.249: [GC (Allocation Failure) [PSYoungGen: 98304K->16368K(114688K)] 127179K->47548K(507904K), 0.0302011 secs] [Times: user=0.08 sys=0.00, real=0.03 secs] 

会发现GC的次数明显减少了很多,但是还是有两次Full GC。不知道是不是和书上所说的一样是因为调用了system.gc()的问题。尝试使用-XX:+DisableExplicitGC参数禁止虚拟机调用system.gc()。

启动eclipse后gc.log如下:

Java HotSpot(TM) 64-Bit Server VM (25.91-b14) for windows-amd64 JRE (1.8.0_91-b14), built on Apr  1 2016 00:58:32 by "java_re" with MS VC++ 10.0 (VS2010)
Memory: 4k page, physical 4080628k(1059332k free), swap 8159380k(4169848k free)
CommandLine flags: -XX:-BytecodeVerificationLocal -XX:-BytecodeVerificationRemote -XX:+DisableExplicitGC -XX:InitialHeapSize=536870912 -XX:MaxHeapSize=536870912 -XX:MaxNewSize=134217728 -XX:NewSize=134217728 -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC 
1.535: [GC (Metadata GC Threshold) [PSYoungGen: 72841K->16360K(114688K)] 72841K->22783K(507904K), 0.0365894 secs] [Times: user=0.11 sys=0.02, real=0.04 secs] 
1.572: [Full GC (Metadata GC Threshold) [PSYoungGen: 16360K->0K(114688K)] [ParOldGen: 6422K->22377K(393216K)] 22783K->22377K(507904K), [Metaspace: 19078K->19078K(1069056K)], 0.0790487 secs] [Times: user=0.30 sys=0.00, real=0.08 secs] 
2.294: [GC (Allocation Failure) [PSYoungGen: 98304K->2180K(114688K)] 120681K->24565K(507904K), 0.0030296 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 
3.984: [GC (Metadata GC Threshold) [PSYoungGen: 99625K->16374K(114688K)] 122010K->45904K(507904K), 0.0233102 secs] [Times: user=0.09 sys=0.00, real=0.02 secs] 
4.008: [Full GC (Metadata GC Threshold) [PSYoungGen: 16374K->0K(114688K)] [ParOldGen: 29529K->37087K(393216K)] 45904K->37087K(507904K), [Metaspace: 32359K->32359K(1081344K)], 0.1461753 secs] [Times: user=0.45 sys=0.00, real=0.15 secs] 
6.111: [GC (Allocation Failure) [PSYoungGen: 98304K->16373K(114688K)] 135391K->62847K(507904K), 0.0206524 secs] [Times: user=0.08 sys=0.00, real=0.02 secs] 
13.101: [GC (Allocation Failure) [PSYoungGen: 114677K->16379K(114688K)] 161151K->71127K(507904K), 0.0175909 secs] [Times: user=0.09 sys=0.00, real=0.02 secs] 
13.593: [GC (Metadata GC Threshold) [PSYoungGen: 51099K->16362K(76800K)] 105847K->73887K(470016K), 0.0205875 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 
13.614: [Full GC (Metadata GC Threshold) [PSYoungGen: 16362K->0K(76800K)] [ParOldGen: 57524K->50904K(393216K)] 73887K->50904K(470016K), [Metaspace: 54073K->54053K(1099776K)], 0.1943444 secs] [Times: user=0.44 sys=0.00, real=0.19 secs] 
15.022: [GC (Allocation Failure) [PSYoungGen: 60416K->10933K(95744K)] 111320K->61845K(488960K), 0.0152336 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
15.659: [GC (Allocation Failure) [PSYoungGen: 71349K->20256K(97280K)] 122261K->71168K(490496K), 0.0194116 secs] [Times: user=0.13 sys=0.00, real=0.02 secs] 
16.210: [GC (Allocation Failure) [PSYoungGen: 82720K->29696K(92672K)] 133632K->80608K(485888K), 0.0294192 secs] [Times: user=0.08 sys=0.00, real=0.03 secs] 
16.758: [GC (Allocation Failure) [PSYoungGen: 92160K->36839K(84992K)] 143072K->88917K(478208K), 0.0286740 secs] [Times: user=0.12 sys=0.00, real=0.03 secs] 
17.782: [GC (Allocation Failure) [PSYoungGen: 84967K->41445K(89600K)] 137045K->93531K(482816K), 0.0539233 secs] [Times: user=0.14 sys=0.00, real=0.05 secs] 
20.031: [GC (Allocation Failure) [PSYoungGen: 89573K->41448K(85504K)] 141659K->96350K(478720K), 0.0565695 secs] [Times: user=0.11 sys=0.00, real=0.06 secs] 

发现Full GC次数反而多了一次,可见因此JDK1.8在这部分内容有所改变,具体原因还在探究中。

四、选择垃圾收集器降低延迟

JDK1.8中默认使用"Parallel Scavenge"和"ParNew"收集器

我们把其中的Parallel Scavenge改成CMS。

参数设置:

-XX:+UseConcMarkSweepGC
-XX:+UseParNewGC

更改收集器前老年代的收集时间如下(25.956ms):

更改收集器后老年代的收集时间如下(10.901ms):

猜你喜欢

转载自blog.csdn.net/Alexwym/article/details/82051960
今日推荐