Java 最大堆内存 500M Kubernetes Pod 最大内存 700M 出现了 OOM

Kubernetes JVM Java About 4,740 words

现象

Java服务部署在Kubernetes中,Pod的内存大小设置为700MJava堆内存设置的-Xmx500M,线上频频发生OOMKillerKubernetes直接Kill掉了。

服务参数

主要的参数如下:

  • Java 11
  • -XX:+UseG1GC
  • -Xms500M
  • -Xmx500M
  • -Xss256K
  • -XX:MaxMetaspaceSize=96M
  • -XX:MaxDirectMemorySize=64M
  • -XX:+UseStringDeduplication
  • Kubernetes Pod Memory Limit: 700Mi

初步分析

查看堆内存占用,未发现异常。

使用Prometheus监控,未发现异常。

NMT 监测

开启了Native Memory Tracing后再次检测。

bash-4.4$ jcmd 1 VM.native_memory
1:

Native Memory Tracking:

(Omitting categories weighting less than 1KB)

Total: reserved=1046250KB, committed=721206KB
       malloc: 45058KB #421581
       mmap:   reserved=1001192KB, committed=676148KB

-                 Java Heap (reserved=512000KB, committed=512000KB)
                            (mmap: reserved=512000KB, committed=512000KB) 

-                     Class (reserved=83253KB, committed=9141KB)
                            (classes #13817)
                            (  instance classes #12903, array classes #914)
                            (malloc=1333KB #28086) 
                            (mmap: reserved=81920KB, committed=7808KB) 
                            (  Metadata:   )
                            (    reserved=65536KB, committed=55936KB)
                            (    used=55654KB)
                            (    waste=282KB =0.50%)
                            (  Class space:)
                            (    reserved=81920KB, committed=7808KB)
                            (    used=7503KB)
                            (    waste=305KB =3.91%)

-                    Thread (reserved=29948KB, committed=3724KB)
                            (thread #51)
                            (stack: reserved=29816KB, committed=3592KB)
                            (malloc=75KB #304) 
                            (arena=57KB #98)

-                      Code (reserved=249993KB, committed=35205KB)
                            (malloc=2305KB #11509) 
                            (mmap: reserved=247688KB, committed=32900KB) 

-                        GC (reserved=69547KB, committed=69547KB)
                            (malloc=17647KB #13618) 
                            (mmap: reserved=51900KB, committed=51900KB) 

-                 GCCardSet (reserved=124KB, committed=124KB)
                            (malloc=124KB #1531) 

-                  Compiler (reserved=423KB, committed=423KB)
                            (malloc=259KB #1161) 
                            (arena=165KB #5)

-                  Internal (reserved=551KB, committed=551KB)
                            (malloc=547KB #15609) 
                            (mmap: reserved=4KB, committed=4KB) 

-                     Other (reserved=112KB, committed=112KB)
                            (malloc=112KB #21) 

-                    Symbol (reserved=13106KB, committed=13106KB)
                            (malloc=11819KB #327453) 
                            (arena=1287KB #1)

-    Native Memory Tracking (reserved=7022KB, committed=7022KB)
                            (malloc=435KB #6199) 
                            (tracking overhead=6587KB)

-        Shared class space (reserved=12288KB, committed=11968KB)
                            (mmap: reserved=12288KB, committed=11968KB) 

-               Arena Chunk (reserved=197KB, committed=197KB)
                            (malloc=197KB) 

-                   Tracing (reserved=32KB, committed=32KB)
                            (arena=32KB #1)

-                    Module (reserved=86KB, committed=86KB)
                            (malloc=86KB #1381) 

-                 Safepoint (reserved=8KB, committed=8KB)
                            (mmap: reserved=8KB, committed=8KB) 

-           Synchronization (reserved=131KB, committed=131KB)
                            (malloc=131KB #1416) 

-            Serviceability (reserved=18KB, committed=18KB)
                            (malloc=18KB #17) 

-                 Metaspace (reserved=65798KB, committed=56198KB)
                            (malloc=262KB #120) 
                            (mmap: reserved=65536KB, committed=55936KB) 

-      String Deduplication (reserved=1580KB, committed=1580KB)
                            (malloc=1580KB #12988) 

-                   Unknown (reserved=32KB, committed=32KB)
                            (mmap: reserved=32KB, committed=32KB) 

再次分析

以上数据为探测数据,NMT能监测的内存总和为721206KB

使用MXBeanManagementAgent可获取NIO操作的Buffer Pool堆外内存(这部分不在NMT采集范围)。

分析NMT发现,committed的大部分内存由以下几个方面组成:

  • Heap: 512000KB(500M)
  • Code: 35205KB(34.3M)
  • GC: 69547KB(67.9M)
  • Symbol: 13106KB(12.7M)
  • Shared class space: 11968KB(11.7M)
  • Metaspace: 56198KB(54.9M)

结论

  • 对于Web应用,会有更多的线程开辟出来。
  • Metaspace会占用较大内存。
  • G1GC会占用较大内存。
  • Code占用会随着C1C2编译器的热点代码编译,会占用更多空间。
  • NMT无法监测堆外内存DirectedMapped

解决方法

  1. Kubernetes的内存限制:扩大内存。
  2. JVM堆内存降低。
  3. 使用SerialGC,降低GC的内存占用。
Views: 277 · Posted: 2023-12-25

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh