這篇文章主要講解了“SpringBoot項(xiàng)目中優(yōu)化和垃圾回收器的對(duì)比分析”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“SpringBoot項(xiàng)目中優(yōu)化和垃圾回收器的對(duì)比分析”吧!
滄源ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!一、SpringBoot項(xiàng)目在外部Tomcat啟動(dòng)時(shí)加載兩次
如下所示,spring標(biāo)志出現(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.42\apache-tomcat-8.5.42\webapps\kill-0.0.1-SNAPSHOT\WEB-INF\classes started by pitt in C:\Users\pitt\AppData\Local\MyEclipse 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.RELEASE 2020-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: default 2020-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 hierarchy 2020-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.42\apache-tomcat-8.5.42\webapps\server\WEB-INF\classes started by pitt in C:\Users\pitt\AppData\Local\MyEclipse 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.RELEASE 2020-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: default 2020-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 hierarchy 2020-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)
加載時(shí)間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
原因:打開(kāi)配置文件server.xml,Tomcat在啟動(dòng)web項(xiàng)目時(shí),會(huì)首先在Host標(biāo)簽中找webapps下的項(xiàng)目,進(jìn)行加載后,又會(huì)在Context 標(biāo)簽中再次加載,導(dǎo)致加載兩次
解決: 將appBase="webapps"替換為appBase = " "
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Context docBase="D:\apache-tomcat-8.5.42\apache-tomcat-8.5.42\webapps\server" path="" reloadable="true" source="org.eclipse.jst.jee.server:server"/>
加載一次時(shí)間為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àn)證過(guò)程
對(duì)類的加載時(shí)間進(jìn)行優(yōu)化,首先查看類的加載時(shí)間
C:\Program Files\Java\jdk1.8.0_211\bin>jps 16612 QuorumPeerMain 9172 Main 19416 MyEclipse 952 Bootstrap 19580 Jps C:\Program Files\Java\jdk1.8.0_211\bin>jstat -class 952 Loaded Bytes Unloaded Bytes Time 10533 20062.0 0 0.0 7.83
我們默認(rèn)通過(guò)MyEclipse編譯的代碼是可靠的,不需要在加載的時(shí)候進(jìn)行字節(jié)碼驗(yàn)證,使用-Xverify:none參數(shù)將字節(jié)碼驗(yàn)證過(guò)程禁用掉,來(lái)提升類的加載速度
C:\Program Files\Java\jdk1.8.0_211\bin>jstat -class 12308 Loaded Bytes Unloaded Bytes Time 9822 18874.2 0 0.0 6.70
提升效果一般,只減少了1s
此外由于HotSpot采用了JIT編譯器可以提前加載熱代碼,1.8版本JVM的代碼編譯速度已經(jīng)比較優(yōu)秀,此處不做優(yōu)化
三、調(diào)整內(nèi)存設(shè)置控制垃圾收集頻率
Java8默認(rèn)的GC回收器為Parallel Scavenge + Parallel Old
用Jmeter發(fā)送請(qǐng)求作為輔助測(cè)試
可以看到,測(cè)試時(shí)間內(nèi),發(fā)起了103次Minor GC,耗時(shí)不到1s,其實(shí)時(shí)間停頓并不多,但是5次Full GC占用了很大部分的GC時(shí)間,主要針對(duì)這部分時(shí)間進(jìn)行優(yōu)化。通過(guò)-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都伴隨著老年代的擴(kuò)容,日志還顯示有時(shí)候內(nèi)存回收狀況不理想,獲取可用的內(nèi)存主要通過(guò)空間的擴(kuò)容。
查看運(yùn)行期間的CPU使用情況,如下圖:
垃圾回收的CPU使用率曲線幾乎看不到,CPU資源還有可利用的余地,為此想到更換原有的垃圾回收器來(lái)進(jìn)行GC優(yōu)化
CMS:
相比于默認(rèn)的Parallel GC明顯改善了Full GC所消耗的時(shí)間,Stop The World時(shí)間減少,但是Minor GC大量增加,嘗試調(diào)整新生代的大小來(lái)減少M(fèi)inor GC的產(chǎn)生,在使用-XX:NewRatio=1 -XX:SurvivorRatio=3
-XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=85參數(shù)進(jìn)行調(diào)整后,Minor GC數(shù)量下降,但是相比而言還是很多
G1:
G1為最新的垃圾回收算法,將新生代和老年代同時(shí)進(jìn)行標(biāo)記和回收,采用標(biāo)記-整理算法,不會(huì)產(chǎn)生內(nèi)存碎片,在時(shí)間消耗上表現(xiàn)的最好,平均每次回收占用的時(shí)間最短,并且最好在內(nèi)存比較大的機(jī)器上使用G1算法進(jìn)行回收
G1的優(yōu)勢(shì)在于:
作為CMS的長(zhǎng)期替代品
1、G1是一個(gè)壓縮收集器,提供足夠強(qiáng)的壓縮來(lái)完全避免狹小的內(nèi)存分配
2、依賴Regions概念,大大簡(jiǎn)化收集器邏輯,大部分情況下規(guī)避潛在的內(nèi)存碎片問(wèn)題
3、比CMS的GC停頓時(shí)長(zhǎng)更加可預(yù)測(cè),并允許用戶指定停頓時(shí)長(zhǎng)
總結(jié):
以上三種回收器都為多線程垃圾回收器,但是對(duì)于不同的環(huán)境,應(yīng)該具體問(wèn)題具體分析,而不是采用一刀切的方法,解決問(wèn)題的過(guò)程即為尋找最優(yōu)解的過(guò)程。
感謝各位的閱讀,以上就是“SpringBoot項(xiàng)目中優(yōu)化和垃圾回收器的對(duì)比分析”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)SpringBoot項(xiàng)目中優(yōu)化和垃圾回收器的對(duì)比分析這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)建站,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站aaarwkj.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
標(biāo)題名稱:SpringBoot項(xiàng)目中優(yōu)化和垃圾回收器的對(duì)比分析-創(chuàng)新互聯(lián)
文章來(lái)源:http://aaarwkj.com/article12/ccjhdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站排名、企業(yè)網(wǎng)站制作、域名注冊(cè)、響應(yīng)式網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容