博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊jvm的CompressedClassSpace
阅读量:6222 次
发布时间:2019-06-21

本文共 8426 字,大约阅读时间需要 28 分钟。

本文主要研究一下jvm的CompressedClassSpace

CompressedClassSpace

  • java8移除了permanent generation,然后class metadata存储在native memory中,其大小默认是不受限的,可以通过-XX:MaxMetaspaceSize来限制
  • 如果开启了-XX:+UseCompressedOops及-XX:+UseCompressedClassesPointers(默认是开启),则UseCompressedOops会使用32-bit的offset来代表java object的引用,而UseCompressedClassPointers则使用32-bit的offset来代表64-bit进程中的class pointer;可以使用CompressedClassSpaceSize来设置这块的空间大小
  • 如果开启了指针压缩,则CompressedClassSpace分配在MaxMetaspaceSize里头,即MaxMetaspaceSize=Compressed Class Space Size + Metaspace area (excluding the Compressed Class Space) Size

查看CompressedClassSpace大小

jcmd pid GC.heap_info

/ # jcmd 1 GC.heap_info1:Shenandoah Heap 524288K total, 144896K committed, 77232K used 2048 x 256K regionsStatus: not cancelledReserved region: - [0x00000000e0000000, 0x0000000100000000) Metaspace       used 45675K, capacity 46867K, committed 47104K, reserved 1091584K  class space    used 5406K, capacity 5838K, committed 5888K, reserved 1048576K复制代码

可以看到整个metaspace使用了45675K,其中class space使用了5406K,而Metaspace area (excluding the Compressed Class Space)使用了45675K-5406K=40269K;整个metaspace的reserved大小为1091584K,其中class space的reserved大小为1048576K

jcmd pid VM.native_memory

/ # jcmd 1 VM.native_memory1:Native Memory Tracking:Total: reserved=2224403KB, committed=238187KB-                 Java Heap (reserved=524288KB, committed=144896KB)                            (mmap: reserved=524288KB, committed=144896KB)-                     Class (reserved=1092940KB, committed=48460KB)                            (classes #8563)                            (  instance classes #7988, array classes #575)                            (malloc=1356KB #20589)                            (mmap: reserved=1091584KB, committed=47104KB)                            (  Metadata:   )                            (    reserved=43008KB, committed=41216KB)                            (    used=40286KB)                            (    free=930KB)                            (    waste=0KB =0.00%)                            (  Class space:)                            (    reserved=1048576KB, committed=5888KB)                            (    used=5407KB)                            (    free=481KB)                            (    waste=0KB =0.00%)-                    Thread (reserved=37130KB, committed=2846KB)                            (thread #36)                            (stack: reserved=36961KB, committed=2676KB)                            (malloc=127KB #189)                            (arena=42KB #70)-                      Code (reserved=529360KB, committed=15420KB)                            (malloc=968KB #4745)                            (mmap: reserved=528392KB, committed=14452KB)-                        GC (reserved=21844KB, committed=7724KB)                            (malloc=5460KB #9644)                            (mmap: reserved=16384KB, committed=2264KB)-                  Compiler (reserved=165KB, committed=165KB)                            (malloc=34KB #455)                            (arena=131KB #5)-                  Internal (reserved=3758KB, committed=3758KB)                            (malloc=1710KB #6582)                            (mmap: reserved=2048KB, committed=2048KB)-                     Other (reserved=32KB, committed=32KB)                            (malloc=32KB #3)-                    Symbol (reserved=10277KB, committed=10277KB)                            (malloc=7456KB #225421)                            (arena=2821KB #1)-    Native Memory Tracking (reserved=4235KB, committed=4235KB)                            (malloc=10KB #126)                            (tracking overhead=4225KB)-               Arena Chunk (reserved=176KB, committed=176KB)                            (malloc=176KB)-                   Logging (reserved=7KB, committed=7KB)                            (malloc=7KB #264)-                 Arguments (reserved=18KB, committed=18KB)                            (malloc=18KB #500)-                    Module (reserved=165KB, committed=165KB)                            (malloc=165KB #1708)-                 Safepoint (reserved=4KB, committed=4KB)                            (mmap: reserved=4KB, committed=4KB)-                   Unknown (reserved=4KB, committed=4KB)                            (mmap: reserved=4KB, committed=4KB)复制代码

可以看到class部分,reserved大小为1092940KB,其中Metadata的reserved大小为43008KB,Class space的reserved大小为1048576KB;其中Metadata使用了40286KB,而Class space使用了5407KB

jmx查看

@GetMapping("/meta")    public Object getMetaspaceSize(){        return ManagementFactory.getPlatformMXBeans(MemoryPoolMXBean.class)                .stream()                .filter(e -> MemoryType.NON_HEAP == e.getType())                .filter(e -> e.getName().equals("Metaspace") || e.getName().equals("Compressed Class Space"))                .map(e -> "name:"+e.getName()+",info:"+e.getUsage())                .collect(Collectors.toList());    }复制代码

输出如下:

/ # curl -i localhost:8080/memory/metaHTTP/1.1 200Content-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedDate: Sun, 31 Mar 2019 03:06:55 GMT["name:Metaspace,info:init = 0(0K) used = 46236784(45153K) committed = 47710208(46592K) max = -1(-1K)","name:Compressed Class Space,info:init = 0(0K) used = 5482736(5354K) committed = 6029312(5888K) max = 1073741824(1048576K)"]复制代码

这里可以看到Metaspace总共使用了45153K,其中Compressed Class Space部分使用了5354K,而Metaspace area (excluding the Compressed Class Space)使用了45153K-5354K=39799K;而这里显示的Metaspace的max为-1,其中Compressed Class Space部分max值为1048576K即1G

spring boot应用查看

/ # curl -i "http://localhost:8080/actuator/metrics/jvm.memory.used?tag=area:nonheap"HTTP/1.1 200Content-Disposition: inline;filename=f.txtContent-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8Transfer-Encoding: chunkedDate: Sun, 31 Mar 2019 02:52:51 GMT{
"name":"jvm.memory.used","description":"The amount of used memory","baseUnit":"bytes","measurements":[{
"statistic":"VALUE","value":6.4449464E7}],"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:Metaspace"HTTP/1.1 200Content-Disposition: inline;filename=f.txtContent-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8Transfer-Encoding: chunkedDate: Sun, 31 Mar 2019 02:54:56 GMT{
"name":"jvm.memory.used","description":"The amount of used memory","baseUnit":"bytes","measurements":[{
"statistic":"VALUE","value":4.7468312E7}],"availableTags":[]}/ # curl -i "http://localhost:8080/actuator/metrics/jvm.memory.used?tag=area:nonheap&tag=id:Compressed%20Class%20Space"HTTP/1.1 200Content-Disposition: inline;filename=f.txtContent-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8Transfer-Encoding: chunkedDate: Sun, 31 Mar 2019 02:55:18 GMT{"name":"jvm.memory.used","description":"The amount of used memory","baseUnit":"bytes","measurements":[{"statistic":"VALUE","value":5609952.0}],"availableTags":[]}复制代码

springboot使用micrometer,通过/actuator/metrics接口提供相关指标查询功能,其中Metaspace及Compressed Class Space在jvm.memory.used这个metric中 它是基于MemoryPoolMXBean来实现的,具体详见micrometer-core-1.1.3-sources.jar!/io/micrometer/core/instrument/binder/jvm/JvmMemoryMetrics.java

小结

  • java8移除了permanent generation,然后class metadata存储在native memory中,其大小默认是不受限的,可以通过-XX:MaxMetaspaceSize来限制;如果开启了-XX:+UseCompressedOops及-XX:+UseCompressedClassesPointers(默认是开启),则UseCompressedOops会使用32-bit的offset来代表java object的引用,而UseCompressedClassPointers则使用32-bit的offset来代表64-bit进程中的class pointer;可以使用CompressedClassSpaceSize来设置这块的空间大小
  • 开启了指针压缩,则CompressedClassSpace分配在MaxMetaspaceSize里头,即MaxMetaspaceSize=Compressed Class Space Size + Metaspace area (excluding the Compressed Class Space) Size
  • 查看CompressedClassSpace的内存使用情况有好几种方法:
    • jcmd pid GC.heap_info(Metaspace为总的部分,包含了class space,而Metaspace area (excluding the Compressed Class Space)需要自己计算即total-class space)
    • jcmd pid VM.native_memory(class为总的部分,包含了Metaspace area (excluding the Compressed Class Space)及Class Space)
    • 使用JMX来获取NON_HEAP类型中的name为Metaspace及Compressed Class Space的MemoryPoolMXBean可以得到Metaspace及Compressed Class Space的使用情况(JMX得到的Metaspace为总的部分,而Metaspace area (excluding the Compressed Class Space)需要自己计算即total-class space)
    • 如果是springboot应用,它使用micrometer,通过/actuator/metrics接口提供相关指标查询功能,其中Metaspace及Compressed Class Space在jvm.memory.used这个metric中

doc

转载于:https://juejin.im/post/5ca033f5f265da30807fe126

你可能感兴趣的文章
c/c++中保留两位有效数字
查看>>
ElasticSearch 2 (32) - 信息聚合系列之范围限定
查看>>
VS2010远程调试C#程序
查看>>
[MicroPython]TurniBit开发板DIY自动窗帘模拟系统
查看>>
由String类的Split方法所遇到的两个问题
查看>>
Python3.4 12306 2015年3月验证码识别
查看>>
从Handler.post(Runnable r)再一次梳理Android的消息机制(以及handler的内存泄露)
查看>>
windows查看端口占用
查看>>
Yii用ajax实现无刷新检索更新CListView数据
查看>>
JDBC的事务
查看>>
Io流的概述
查看>>
App 卸载记录
查看>>
JavaScript变量和作用域
查看>>
JS 对象机制深剖——new 运算符
查看>>
开源SIP服务器加密软件NethidPro升级
查看>>
大型网站技术架构(一)大型网站架构演化
查看>>
百度页面分享插件源代码
查看>>
《别做正常的傻瓜》的一些读书心得
查看>>
作业:实现简单的shell sed替换功能和修改haproxy配置文件
查看>>
spring配置多数据源问题
查看>>