SpringBoot項目調優(yōu)及垃圾回收器的比較詳解
一、SpringBoot項目在外部Tomcat啟動時加載兩次
如下所示,spring標志出現(xiàn)兩次(截取部分代碼)
. ____ _ __ _ _ / / ___’_ __ _ _(_)_ __ __ _ ( ( )___ | ’_ | ’_| | ’_ / _` | / ___)| |_)| | | | | || (_| | ) ) ) ) ’ |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.7.RELEASE)2020-04-02 16:57:29.505 INFO 19964 --- [ost-startStop-1] com.pitt.kill.server.MainApplication : Starting MainApplication on LAPTOP-1U9EARRO with PID 19964 (D:apache-tomcat-8.5.42apache-tomcat-8.5.42webappskill-0.0.1-SNAPSHOTWEB-INFclasses started by pitt in C:UserspittAppDataLocalMyEclipse 2017 CI)2020-04-02 16:57:29.508 DEBUG 19964 --- [ost-startStop-1] com.pitt.kill.server.MainApplication : Running with Spring Boot v1.5.7.RELEASE, Spring v4.3.11.RELEASE2020-04-02 16:57:29.509 INFO 19964 --- [ost-startStop-1] com.pitt.kill.server.MainApplication : No active profile set, falling back to default profiles: default2020-04-02 16:57:29.539 INFO 19964 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1e20bc0d: startup date [Thu Apr 02 16:57:29 GMT+08:00 2020]; root of context hierarchy2020-04-02 16:57:30.097 INFO 19964 --- [ost-startStop-1] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [spring/spring-jdbc.xml]2020-04-02 16:57:30.141 WARN 19964 --- [ost-startStop-1] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in ’[com.pitt.kill.model.mapper]’ package. Please check your configuration.2020-04-02 16:57:30.258 WARN 19964 --- [ost-startStop-1] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in ’[com.pitt.kill.server]’ package. Please check your configuration.2020-04-02 16:57:30.307 INFO 19964 --- [ost-startStop-1] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode! . ____ _ __ _ _ / / ___’_ __ _ _(_)_ __ __ _ ( ( )___ | ’_ | ’_| | ’_ / _` | / ___)| |_)| | | | | || (_| | ) ) ) ) ’ |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.7.RELEASE)2020-04-02 16:57:37.069 INFO 19964 --- [ost-startStop-1] com.pitt.kill.server.MainApplication : Starting MainApplication on LAPTOP-1U9EARRO with PID 19964 (D:apache-tomcat-8.5.42apache-tomcat-8.5.42webappsserverWEB-INFclasses started by pitt in C:UserspittAppDataLocalMyEclipse 2017 CI)2020-04-02 16:57:37.072 DEBUG 19964 --- [ost-startStop-1] com.pitt.kill.server.MainApplication : Running with Spring Boot v1.5.7.RELEASE, Spring v4.3.11.RELEASE2020-04-02 16:57:37.072 INFO 19964 --- [ost-startStop-1] com.pitt.kill.server.MainApplication : No active profile set, falling back to default profiles: default2020-04-02 16:57:37.102 INFO 19964 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1565ae71: startup date [Thu Apr 02 16:57:37 GMT+08:00 2020]; root of context hierarchy2020-04-02 16:57:37.675 INFO 19964 --- [ost-startStop-1] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [spring/spring-jdbc.xml]2020-04-02 16:57:37.863 INFO 19964 --- [ost-startStop-1] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!2020-04-02 16:57:38.422 INFO 19964 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean ’org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration’ of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$6cdea02f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)2020-04-02 16:57:38.456 INFO 19964 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean ’shiroConfig’ of type [com.pitt.kill.server.config.ShiroConfig$$EnhancerBySpringCGLIB$$4392bed0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
加載時間30s
2020-04-02 16:57:42.825 INFO 19964 --- [ost-startStop-1] com.pitt.kill.server.MainApplication : Started MainApplication in 6.091 seconds (JVM running for 30.348)2020-04-02 16:57:42.946 INFO 19964 --- [ main] org.apache.catalina.startup.Catalina : Server startup in 29361 ms
原因:打開配置文件server.xml,Tomcat在啟動web項目時,會首先在Host標簽中找webapps下的項目,進行加載后,又會在Context 標簽中再次加載,導致加載兩次解決: 將appBase='webapps'替換為appBase = ' '
<Host appBase='webapps' autoDeploy='true' name='localhost' unpackWARs='true'>
<Context docBase='D:apache-tomcat-8.5.42apache-tomcat-8.5.42webappsserver' path='' reloadable='true' source='org.eclipse.jst.jee.server:server'/>
加載一次時間為12s
2020-04-02 17:03:12.759 INFO 952 --- [ost-startStop-1] com.pitt.kill.server.MainApplication : Started MainApplication in 7.478 seconds (JVM running for 12.532)2020-04-02 17:03:13.100 INFO 952 --- [ main] org.apache.catalina.startup.Catalina : Server startup in 11772 ms
二、禁用字節(jié)碼驗證過程
對類的加載時間進行優(yōu)化,首先查看類的加載時間
C:Program FilesJavajdk1.8.0_211bin>jps16612 QuorumPeerMain9172 Main19416 MyEclipse952 Bootstrap19580 JpsC:Program FilesJavajdk1.8.0_211bin>jstat -class 952Loaded Bytes Unloaded Bytes Time 10533 20062.0 0 0.0 7.83
我們默認通過MyEclipse編譯的代碼是可靠的,不需要在加載的時候進行字節(jié)碼驗證,使用-Xverify:none參數(shù)將字節(jié)碼驗證過程禁用掉,來提升類的加載速度
C:Program FilesJavajdk1.8.0_211bin>jstat -class 12308Loaded Bytes Unloaded Bytes Time 9822 18874.2 0 0.0 6.70
提升效果一般,只減少了1s此外由于HotSpot采用了JIT編譯器可以提前加載熱代碼,1.8版本JVM的代碼編譯速度已經比較優(yōu)秀,此處不做優(yōu)化
三、調整內存設置控制垃圾收集頻率
Java8默認的GC回收器為Parallel Scavenge + Parallel Old
用Jmeter發(fā)送請求作為輔助測試
可以看到,測試時間內,發(fā)起了103次Minor GC,耗時不到1s,其實時間停頓并不多,但是5次Full GC占用了很大部分的GC時間,主要針對這部分時間進行優(yōu)化。通過-Xloggc:gc.log -XX:+PrintGCTimeStamps -XX:+PrintGCDetails參數(shù)打印GC日志,將Full GC挑出如下:
7.934: [Full GC (Metadata GC Threshold) [PSYoungGen: 926K->0K(647680K)] [ParOldGen: 54198K->15711K(65024K)] 55125K->15711K(712704K), [Metaspace: 20674K->20674K(1069056K)], 0.0303162 secs] [Times: user=0.03 sys=0.00, real=0.03 secs] 10.430: [Full GC (Metadata GC Threshold) [PSYoungGen: 3883K->0K(646656K)] [ParOldGen: 20986K->17184K(80896K)] 24870K->17184K(727552K), [Metaspace: 34377K->34377K(1081344K)], 0.0413074 secs] [Times: user=0.28 sys=0.00, real=0.04 secs] 379.971: [Full GC (Metadata GC Threshold) [PSYoungGen: 2528K->0K(93696K)] [ParOldGen: 65885K->58730K(132096K)] 68413K->58730K(225792K), [Metaspace: 57642K->57535K(1101824K)], 0.3908541 secs] [Times: user=0.97 sys=0.00, real=0.39 secs] 390.977: [Full GC (Ergonomics) [PSYoungGen: 8688K->0K(230400K)] [ParOldGen: 129221K->120704K(226304K)] 137909K->120704K(456704K), [Metaspace: 62151K->62115K(1105920K)], 0.1319683 secs] [Times: user=0.50 sys=0.02, real=0.13 secs] 553.770: [Full GC (Ergonomics) [PSYoungGen: 47104K->0K(513536K)] [ParOldGen: 228052K->120789K(249344K)] 275156K->120789K(762880K), [Metaspace: 62330K->62330K(1105920K)], 0.1344527 secs] [Times: user=0.27 sys=0.02, real=0.13 secs]
每一次Full GC都伴隨著老年代的擴容,日志還顯示有時候內存回收狀況不理想,獲取可用的內存主要通過空間的擴容。查看運行期間的CPU使用情況,如下圖:
垃圾回收的CPU使用率曲線幾乎看不到,CPU資源還有可利用的余地,為此想到更換原有的垃圾回收器來進行GC優(yōu)化
CMS:
相比于默認的Parallel GC明顯改善了Full GC所消耗的時間,Stop The World時間減少,但是Minor GC大量增加,嘗試調整新生代的大小來減少Minor GC的產生,在使用-XX:NewRatio=1 -XX:SurvivorRatio=3-XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=85參數(shù)進行調整后,Minor GC數(shù)量下降,但是相比而言還是很多
G1:
G1為最新的垃圾回收算法,將新生代和老年代同時進行標記和回收,采用標記-整理算法,不會產生內存碎片,在時間消耗上表現(xiàn)的最好,平均每次回收占用的時間最短,并且最好在內存比較大的機器上使用G1算法進行回收
G1的優(yōu)勢在于:作為CMS的長期替代品1、G1是一個壓縮收集器,提供足夠強的壓縮來完全避免狹小的內存分配2、依賴Regions概念,大大簡化收集器邏輯,大部分情況下規(guī)避潛在的內存碎片問題3、比CMS的GC停頓時長更加可預測,并允許用戶指定停頓時長
總結:
以上三種回收器都為多線程垃圾回收器,但是對于不同的環(huán)境,應該具體問題具體分析,而不是采用一刀切的方法,解決問題的過程即為尋找最優(yōu)解的過程。
到此這篇關于SpringBoot項目調優(yōu)及垃圾回收器的比較詳解的文章就介紹到這了,更多相關SpringBoot項目調優(yōu)及垃圾回收器內容請搜索好吧啦網以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
