本文主要研究一下HotSpot VM的Native Memory Tracking
java8给HotSpot VM引入了Native Memory Tracking (NMT)特性,可以用于追踪JVM的内部内存使用
-XX:NativeMemoryTracking=summary
使用-XX:NativeMemoryTracking=summary可以用于开启NMT,其中该值默认为off,可以设置summary、detail来开启;开启的话,大概会增加5%-10%的性能消耗
/ # jcmd 1 VM.native_memory summary / # jcmd 1 VM.native_memory summary scale=MB
使用jcmd pid VM.native_memory可以查看,后面可以加summary或者detail,如果是开启summary的,就只能使用summary;其中scale参数可以指定展示的单位,可以为KB或者MB或者GB
/ # jcmd 1 VM.native_memory baseline 1: Baseline succeeded
创建baseline之后可以用summary.diff来对比
/ # jcmd 1 VM.native_memory summary.diff
使用summary.diff来查看跟baseline对比的统计信息
-XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics
使用上述命令可以在jvm shutdown的时候输出整体的native memory统计
/ # jcmd 1 VM.native_memory shutdown 1: Native memory tracking has been turned off
使用jcmd pid VM.native_memory shutdown可以用于关闭NMT;注意使用jcmd关闭之后貌似没有对应jcmd命令来开启
/ # jcmd 1 VM.native_memory summary scale=MB 1: Native Memory Tracking: Total: reserved=2175MB, committed=682MB - Java Heap (reserved=501MB, committed=463MB) (mmap: reserved=501MB, committed=463MB) - Class (reserved=1070MB, committed=50MB) (classes #8801) ( instance classes #8204, array classes #597) (malloc=2MB #24660) (mmap: reserved=1068MB, committed=49MB) ( Metadata: ) ( reserved=44MB, committed=43MB) ( used=42MB) ( free=1MB) ( waste=0MB =0.00%) ( Class space:) ( reserved=1024MB, committed=6MB) ( used=5MB) ( free=0MB) ( waste=0MB =0.00%) - Thread (reserved=228MB, committed=27MB) (thread #226) (stack: reserved=227MB, committed=26MB) (malloc=1MB #1139) - Code (reserved=243MB, committed=17MB) (malloc=1MB #5509) (mmap: reserved=242MB, committed=16MB) - GC (reserved=23MB, committed=15MB) (malloc=8MB #11446) (mmap: reserved=16MB, committed=7MB) - Compiler (reserved=26MB, committed=26MB) (malloc=2MB #1951) (arena=24MB #13) - Internal (reserved=5MB, committed=5MB) (malloc=3MB #9745) (mmap: reserved=2MB, committed=2MB) - Other (reserved=2MB, committed=2MB) (malloc=2MB #202) - Symbol (reserved=10MB, committed=10MB) (malloc=8MB #233939) (arena=3MB #1) - Native Memory Tracking (reserved=5MB, committed=5MB) (tracking overhead=5MB) - Arena Chunk (reserved=63MB, committed=63MB) (malloc=63MB)