百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

聊聊jvm的Code Cache jvm classloader

yuyutoo 2024-10-11 23:54 14 浏览 0 评论

本文主要研究一下jvm的Code Cache

Code Cache

JVM生成的native code存放的内存空间称之为Code Cache;JIT编译、JNI等都会编译代码到native code,其中JIT生成的native code占用了Code Cache的绝大部分空间

相关参数

Codecache Size Options

  • -XX:InitialCodeCacheSize

用于设置初始CodeCache大小

  • -XX:ReservedCodeCacheSize

用于设置Reserved code cache的最大大小,通常默认是240M

  • -XX:CodeCacheExpansionSize

用于设置code cache的expansion size,通常默认是64K

Codecache Flush Options

  • -XX:+UseCodeCacheFlushing

是否在code cache满的时候先尝试清理一下,如果还是不够用再关闭编译,默认开启

Compilation Policy Options

  • -XX:CompileThreshold

方法触发编译时的调用次数,默认是10000

  • -XX:OnStackReplacePercentage

方法中循环执行部分代码的执行次数触发OSR编译时的阈值,默认是140

Compilation Limit Options

  • -XX:MaxInlineLevel

针对嵌套调用的最大内联深度,默认为9

  • -XX:MaxInlineSize

方法可以被内联的最大bytecode大小,默认为35

  • -XX:MinInliningThreshold

方法可以被内联的最小调用次数,默认为250

  • -XX:+InlineSynchronizedMethods

是否允许内联synchronized methods,默认为true

Diagnostic Options

  • -XX:+PrintFlagsFinal(默认没有启用)

用于查看所有可设置的参数及最终值(JDK 6 update 21开始才可以用),默认是不包括diagnostic或experimental系的。如果要在-XX:+PrintFlagsFinal的输出里看到这两种参数的信息,分别需要显式指定-XX:+UnlockDiagnosticVMOptions / -XX:+UnlockExperimentalVMOptions(-XX:+PrintCommandLineFlags 这个参数的作用是显示出VM初始化完毕后所有跟最初的默认值不同的参数及它们的值)

  • -XX:+PrintCodeCache(默认没有启用)

-XX:+PrintCodeCache用于jvm关闭时输出code cache的使用情况

  • -XX:+PrintCodeCacheOnCompilation(默认没有启用)

用于在方法每次被编译时输出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启动参数加上-XX:+PrintCodeCache,可以在jvm关闭时输出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应用查看

/ # 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接口提供相关指标查询功能,其中code cache在jvm.memory.used这个metric中

它是基于MemoryPoolMXBean来实现的,具体详见micrometer-core-1.1.3-sources.jar!/io/micrometer/core/instrument/binder/jvm/JvmMemoryMetrics.java

小结

  • JVM生成的native code存放的内存空间称之为Code Cache;JIT编译、JNI等都会编译代码到native code,其中JIT生成的native code占用了Code Cache的绝大部分空间
  • -XX:ReservedCodeCacheSize用于设置Reserved code cache的最大大小,通常默认是240M;对于有些应用来说240M可能太大,code cache可能都填不满,相当于unconstrained,此时JIT就会继续编译任何它认为可以编译的code
  • 查看Code Cache的内存使用情况有好几种方法:
  • jvm启动参数加上-XX:+PrintCodeCache,可以在jvm关闭时输出code cache的使用情况
  • 使用jcmd的Compiler.codecache,其输出跟-XX:+PrintCodeCache相同;
  • 使用jcmd的VM.native_memory也可以查看code cache的使用情况(Code部分)
  • 使用JMX来获取NON_HEAP类型中的name为CodeHeap开头的MemoryPoolMXBean可以得到code cache的使用情况
  • 如果是springboot应用,它使用micrometer,通过/actuator/metrics接口提供相关指标查询功能,其中code cache在jvm.memory.used这个metric中

doc

  • 15 Codecache Tuning
  • JVM的编译策略
  • What are ReservedCodeCacheSize and InitialCodeCacheSize?
  • Why does the JVM have a maximum inline depth?
  • Code Cache满导致应用性能降低

相关推荐

墨尔本一华裔男子与亚裔男子分别失踪数日 警方寻人

中新网5月15日电据澳洲新快网报道,据澳大利亚维州警察局网站消息,22岁的华裔男子邓跃(Yue‘Peter’Deng,音译)失踪已6天,维州警方于当地时间13日发布寻人通告,寻求公众协助寻找邓跃。华...

网络交友须谨慎!美国犹他州一男子因涉嫌杀害女网友被捕

伊森·洪克斯克(图源网络,侵删)据美国广播公司(ABC)25日报道,美国犹他州一名男子于24日因涉嫌谋杀被捕。警方表示,这名男子主动告知警局,称其杀害了一名在网络交友软件上认识的25岁女子。雷顿警...

一课译词:来龙去脉(来龙去脉 的意思解释)

Mountainranges[Photo/SIPA]“来龙去脉”,汉语成语,本指山脉的走势和去向,现比喻一件事的前因后果(causeandeffectofanevent),可以翻译为“i...

高考重要考点:range(range高考用法)

range可以用作动词,也可以用作名词,含义特别多,在阅读理解中出现的频率很高,还经常作为完形填空的选项,而且在作文中使用是非常好的高级词汇。...

C++20 Ranges:现代范围操作(现代c++白皮书)

1.引言:C++20Ranges库简介C++20引入的Ranges库是C++标准库的重要更新,旨在提供更现代化、表达力更强的方式来处理数据序列(范围,range)。Ranges库基于...

学习VBA,报表做到飞 第二章 数组 2.4 Filter函数

第二章数组2.4Filter函数Filter函数功能与autofilter函数类似,它对一个一维数组进行筛选,返回一个从0开始的数组。...

VBA学习笔记:数组:数组相关函数—Split,Join

Split拆分字符串函数,语法Split(expression,字符,Limit,compare),第1参数为必写,后面3个参数都是可选项。Expression为需要拆分的数据,“字符”就是以哪个字...

VBA如何自定义序列,学会这些方法,让你工作更轻松

No.1在Excel中,自定义序列是一种快速填表机制,如何有效地利用这个方法,可以大大增加工作效率。通常在操作工作表的时候,可能会输入一些很有序的序列,如果一一录入就显得十分笨拙。Excel给出了一种...

Excel VBA入门教程1.3 数组基础(vba数组详解)

1.3数组使用数组和对象时,也要声明,这里说下数组的声明:'确定范围的数组,可以存储b-a+1个数,a、b为整数Dim数组名称(aTob)As数据类型Dimarr...

远程网络调试工具百宝箱-MobaXterm

MobaXterm是一个功能强大的远程网络工具百宝箱,它将所有重要的远程网络工具(SSH、Telnet、X11、RDP、VNC、FTP、MOSH、Serial等)和Unix命令(bash、ls、cat...

AREX:携程新一代自动化回归测试工具的设计与实现

一、背景随着携程机票BU业务规模的不断提高,业务系统日趋复杂,各种问题和挑战也随之而来。对于研发测试团队,面临着各种效能困境,包括业务复杂度高、数据构造工作量大、回归测试全量回归、沟通成本高、测试用例...

Windows、Android、IOS、Web自动化工具选择策略

Windows平台中应用UI自动化测试解决方案AutoIT是开源工具,该工具识别windows的标准控件效果不错,但是当它遇到应用中非标准控件定义的UI元素时往往就无能为力了,这个时候选择silkte...

python自动化工具:pywinauto(python快速上手 自动化)

简介Pywinauto是完全由Python构建的一个模块,可以用于自动化Windows上的GUI应用程序。同时,它支持鼠标、键盘操作,在元素控件树较复杂的界面,可以辅助我们完成自动化操作。我在...

时下最火的 Airtest 如何测试手机 APP?

引言Airtest是网易出品的一款基于图像识别的自动化测试工具,主要应用在手机APP和游戏的测试。一旦使用了这个工具进行APP的自动化,你就会发现自动化测试原来是如此简单!!连接手机要进行...

【推荐】7个最强Appium替代工具,移动App自动化测试必备!

在移动应用开发日益火爆的今天,自动化测试成为了确保应用质量和用户体验的关键环节。Appium作为一款广泛应用的移动应用自动化测试工具,为测试人员所熟知。然而,在不同的测试场景和需求下,还有许多其他优...

取消回复欢迎 发表评论: