Java jvm中Code Cache案例詳解
Code Cache
JVM生成的native code存放的內(nèi)存空間稱之為Code Cache;JIT編譯、JNI等都會編譯代碼到native code,其中JIT生成的native code占用了Code Cache的絕大部分空間
相關(guān)參數(shù)
Codecache Size Options
- -XX:InitialCodeCacheSize
用于設(shè)置初始CodeCache大小
- -XX:ReservedCodeCacheSize
用于設(shè)置Reserved code cache的最大大小,通常默認(rèn)是240M
- -XX:CodeCacheExpansionSize
用于設(shè)置code cache的expansion size,通常默認(rèn)是64K
Codecache Flush Options
- -XX:+UseCodeCacheFlushing
是否在code cache滿的時候先嘗試清理一下,如果還是不夠用再關(guān)閉編譯,默認(rèn)開啟
Compilation Policy Options
- -XX:CompileThreshold
方法觸發(fā)編譯時的調(diào)用次數(shù),默認(rèn)是10000
- -XX:OnStackReplacePercentage
方法中循環(huán)執(zhí)行部分代碼的執(zhí)行次數(shù)觸發(fā)OSR編譯時的閾值,默認(rèn)是140
Compilation Limit Options
- -XX:MaxInlineLevel
針對嵌套調(diào)用的最大內(nèi)聯(lián)深度,默認(rèn)為9
- -XX:MaxInlineSize
方法可以被內(nèi)聯(lián)的最大bytecode大小,默認(rèn)為35
- -XX:MinInliningThreshold
方法可以被內(nèi)聯(lián)的最小調(diào)用次數(shù),默認(rèn)為250
- -XX:+InlineSynchronizedMethods
是否允許內(nèi)聯(lián)synchronized methods,默認(rèn)為true
Diagnostic Options
- -XX:+PrintFlagsFinal(默認(rèn)沒有啟用)
用于查看所有可設(shè)置的參數(shù)及最終值(JDK 6 update 21開始才可以用),默認(rèn)是不包括diagnostic或experimental系的。如果要在-XX:+PrintFlagsFinal的輸出里看到這兩種參數(shù)的信息,分別需要顯式指定-XX:+UnlockDiagnosticVMOptions / -XX:+UnlockExperimentalVMOptions(-XX:+PrintCommandLineFlags 這個參數(shù)的作用是顯示出VM初始化完畢后所有跟最初的默認(rèn)值不同的參數(shù)及它們的值)
- -XX:+PrintCodeCache(默認(rèn)沒有啟用)
-XX:+PrintCodeCache用于jvm關(guān)閉時輸出code cache的使用情況
- -XX:+PrintCodeCacheOnCompilation(默認(rèn)沒有啟用)
用于在方法每次被編譯時輸出code cache的使用情況
查看Code Cache的使用情況
-XX:+PrintCodeCache
CodeHeap 'non-profiled nmethods': size=120032Kb used=2154Kb max_used=2160Kb free=117877Kb bounds [0x00000001178ea000, 0x0000000117b5a000, 0x000000011ee22000] CodeHeap 'profiled nmethods': size=120028Kb used=10849Kb max_used=11005Kb free=109178Kb bounds [0x00000001103b3000, 0x0000000110e73000, 0x00000001178ea000] CodeHeap 'non-nmethods': size=5700Kb used=1177Kb max_used=1239Kb free=4522Kb bounds [0x000000010fe22000, 0x0000000110092000, 0x00000001103b3000] total_blobs=5638 nmethods=4183 adapters=435 compilation: enabled stopped_count=0, restarted_count=0 full_count=0
- jvm啟動參數(shù)加上-XX:+PrintCodeCache,可以在jvm關(guān)閉時輸出code cache的使用情況
- 這里分了non-profiled nmethods、profiled nmethods、non-nmethods三部分來展示
- 其中size就是限制的最大大小,used表示實際使用量,max_used就是使用大小的high water mark,free由size-used得來
jcmd pid Compiler.codecache
/ # jcmd 1 Compiler.codecache 1: CodeHeap 'non-profiled nmethods': size=120036Kb used=1582Kb max_used=1582Kb free=118453Kb bounds [0x00007f1e42226000, 0x00007f1e42496000, 0x00007f1e4975f000] CodeHeap 'profiled nmethods': size=120032Kb used=9621Kb max_used=9621Kb free=110410Kb bounds [0x00007f1e3acee000, 0x00007f1e3b65e000, 0x00007f1e42226000] CodeHeap 'non-nmethods': size=5692Kb used=1150Kb max_used=1198Kb free=4541Kb bounds [0x00007f1e3a75f000, 0x00007f1e3a9cf000, 0x00007f1e3acee000] total_blobs=5610 nmethods=4369 adapters=412 compilation: enabled stopped_count=0, restarted_count=0 full_count=0
使用jcmd的Compiler.codecache也可以查看code cache的使用情況,輸出跟-XX:+PrintCodeCache相同
jcmd pid VM.native_memory
/ # jcmd 1 VM.native_memory 1: Native Memory Tracking: Total: reserved=1928023KB, committed=231182KB - Java Heap (reserved=511488KB, committed=140288KB) (mmap: reserved=511488KB, committed=140288KB) - Class (reserved=1090832KB, committed=46608KB) (classes #8218) ( instance classes #7678, array classes #540) (malloc=1296KB #19778) (mmap: reserved=1089536KB, committed=45312KB) ( Metadata: ) ( reserved=40960KB, committed=39680KB) ( used=38821KB) ( free=859KB) ( waste=0KB =0.00%) ( Class space:) ( reserved=1048576KB, committed=5632KB) ( used=5190KB) ( free=442KB) ( waste=0KB =0.00%) - Thread (reserved=37130KB, committed=2806KB) (thread #36) (stack: reserved=36961KB, committed=2636KB) (malloc=127KB #189) (arena=42KB #70) - Code (reserved=248651KB, committed=15351KB) (malloc=963KB #4600) (mmap: reserved=247688KB, committed=14388KB) - GC (reserved=21403KB, committed=7611KB) (malloc=5419KB #9458) (mmap: reserved=15984KB, committed=2192KB) - Compiler (reserved=150KB, committed=150KB) (malloc=20KB #447) (arena=131KB #5) - Internal (reserved=3744KB, committed=3744KB) (malloc=1696KB #6416) (mmap: reserved=2048KB, committed=2048KB) - Other (reserved=24KB, committed=24KB) (malloc=24KB #2) - Symbol (reserved=10094KB, committed=10094KB) (malloc=7305KB #219914) (arena=2789KB #1) - Native Memory Tracking (reserved=4130KB, committed=4130KB) (malloc=12KB #158) (tracking overhead=4119KB) - Arena Chunk (reserved=177KB, committed=177KB) (malloc=177KB) - Logging (reserved=7KB, committed=7KB) (malloc=7KB #264) - Arguments (reserved=18KB, committed=18KB) (malloc=18KB #500) - Module (reserved=165KB, committed=165KB) (malloc=165KB #1699) - Safepoint (reserved=4KB, committed=4KB) (mmap: reserved=4KB, committed=4KB) - Unknown (reserved=4KB, committed=4KB) (mmap: reserved=4KB, committed=4KB)
使用jcmd的VM.native_memory也可以查看code cache的使用情況(Code部分),Compiler部分為Memory tracking used by the compiler when generating code
使用MemoryPoolMXBean查看
@Test public void testGetCodeCacheUsage(){ ManagementFactory.getPlatformMXBeans(MemoryPoolMXBean.class) .stream() .filter(e -> MemoryType.NON_HEAP == e.getType()) .filter(e -> e.getName().startsWith("CodeHeap")) .forEach(e -> { LOGGER.info("name:{},info:{}",e.getName(),e.getUsage()); }); }
MemoryPoolMXBean包含了HEAP及NON_HEAP,其中code cache屬于NON_HEAP,其輸出如下:
12:21:10.728 [main] INFO com.example.CodeCacheTest - name:CodeHeap 'non-nmethods',info:init = 2555904(2496K) used = 1117696(1091K) committed = 2555904(2496K) max = 5836800(5700K)
12:21:10.743 [main] INFO com.example.CodeCacheTest - name:CodeHeap 'profiled nmethods',info:init = 2555904(2496K) used = 1543808(1507K) committed = 2555904(2496K) max = 122908672(120028K)
12:21:10.743 [main] INFO com.example.CodeCacheTest - name:CodeHeap 'non-profiled nmethods',info:init = 2555904(2496K) used = 319616(312K) committed = 2555904(2496K) max = 122912768(120032K)
spring boot應(yīng)用查看
/ # curl -i "http://localhost:8080/actuator/metrics/jvm.memory.used?tag=area:nonheap" HTTP/1.1 200 Content-Disposition: inline;filename=f.txt Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8 Transfer-Encoding: chunked Date: Sat, 30 Mar 2019 04:26:39 GMT {"name":"jvm.memory.used","description":"The amount of used memory","baseUnit":"bytes","measurements":[{"statistic":"VALUE","value":6.5295408E7}],"availableTags":[{"tag":"id","values":["CodeHeap 'non-profiled nmethods'","CodeHeap 'profiled nmethods'","Compressed Class Space","Metaspace","CodeHeap 'non-nmethods'"]}]} / # curl -i "http://localhost:8080/actuator/metrics/jvm.memory.used?tag=area:nonheap&tag=id:CodeHeap%20%27non-profiled %20nmethods%27" HTTP/1.1 200 Content-Disposition: inline;filename=f.txt Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8 Transfer-Encoding: chunked Date: Sat, 30 Mar 2019 04:24:58 GMT {"name":"jvm.memory.used","description":"The amount of used memory","baseUnit":"bytes","measurements":[{"statistic":"VALUE","value":1592448.0}],"availableTags":[]}
springboot使用micrometer,通過/actuator/metrics接口提供相關(guān)指標(biāo)查詢功能,其中code cache在jvm.memory.used這個metric中
它是基于MemoryPoolMXBean來實現(xiàn)的,具體詳見micrometer-core-1.1.3-sources.jar!/io/micrometer/core/instrument/binder/jvm/JvmMemoryMetrics.java
小結(jié)
- JVM生成的native code存放的內(nèi)存空間稱之為Code Cache;JIT編譯、JNI等都會編譯代碼到native code,其中JIT生成的native code占用了Code Cache的絕大部分空間
- -XX:ReservedCodeCacheSize用于設(shè)置Reserved code cache的最大大小,通常默認(rèn)是240M;對于有些應(yīng)用來說240M可能太大,code cache可能都填不滿,相當(dāng)于unconstrained,此時JIT就會繼續(xù)編譯任何它認(rèn)為可以編譯的code
- 查看Code Cache的內(nèi)存使用情況有好幾種方法:
- jvm啟動參數(shù)加上-XX:+PrintCodeCache,可以在jvm關(guān)閉時輸出code cache的使用情況
- 使用jcmd的Compiler.codecache,其輸出跟-XX:+PrintCodeCache相同;
- 使用jcmd的VM.native_memory也可以查看code cache的使用情況(
Code部分
) - 使用JMX來獲取NON_HEAP類型中的name為CodeHeap開頭的MemoryPoolMXBean可以得到code cache的使用情況
- 如果是springboot應(yīng)用,它使用micrometer,通過/actuator/metrics接口提供相關(guān)指標(biāo)查詢功能,其中code cache在jvm.memory.used這個metric中
doc
What are ReservedCodeCacheSize and InitialCodeCacheSize?
Why does the JVM have a maximum inline depth?
Code Cache滿導(dǎo)致應(yīng)用性能降低
到此這篇關(guān)于Java jvm中Code Cache案例詳解的文章就介紹到這了,更多相關(guān)Java jvm中Code Cache內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring mvc中的@PathVariable動態(tài)參數(shù)詳解
這篇文章主要介紹了spring mvc中的@PathVariable動態(tài)參數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能
這篇文章主要介紹了SpringBoot+Vue.js實現(xiàn)前后端分離的文件上傳功能,需要的朋友可以參考下2018-06-06java讀寫ini文件、FileOutputStream問題
這篇文章主要介紹了java讀寫ini文件、FileOutputStream問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04Springboot集成kafka高級應(yīng)用實戰(zhàn)分享
這篇文章主要介紹了Springboot集成kafka高級應(yīng)用實戰(zhàn)分享,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08使用ServletInputStream()輸入流讀取圖片方式
這篇文章主要介紹了使用ServletInputStream()輸入流讀取圖片方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10JDBC 實現(xiàn)通用的增刪改查基礎(chǔ)類方法
下面小編就為大家分享一篇JDBC 實現(xiàn)通用的增刪改查基礎(chǔ)類方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01