Java 不同垃圾回收器占用的内存大小

GC JVM Java About 7,694 words

需求

Java内存中除了堆内存外,还有堆外内存,如:垃圾回收器就会占用一定的堆外内存。

对于在Kubernetes中部署的服务,如果频繁被OOM,需要关注堆外内存。GC也是占用内存的一部分。

基准参数

  • 最大堆内存:2G
  • 最小堆内存:2G

结论

GC 占用内存
SerialGC 6.7M
ParallelGC 106M
G1GC 133M
ShenandoahGC 37M~69M
ZGC 96M~8256M

SerialGC

java -Xmx2G -Xms2G -XX:+UseSerialGC -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version

NMT显示GC占用:7034970字节(6.7M)

❯ java -Xmx2G -Xms2G -XX:+UseSerialGC -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version
-XX:InitialHeapSize=2147483648 -XX:MaxHeapSize=2147483648 -XX:NativeMemoryTracking=summary -XX:+PrintCommandLineFlags -XX:+PrintNMTStatistics -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:+UnlockDiagnosticVMOptions -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseSerialGC
VM settings:
    Min. Heap Size: 2.00G
    Max. Heap Size: 2.00G
    Using VM: OpenJDK 64-Bit Server VM

openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment Temurin-11.0.17+8 (build 11.0.17+8)
OpenJDK 64-Bit Server VM Temurin-11.0.17+8 (build 11.0.17+8, mixed mode)

Native Memory Tracking:

Total: reserved=3514969375, committed=2191551775
-                 Java Heap (reserved=2147483648, committed=2147483648)
                            (mmap: reserved=2147483648, committed=2147483648)

-                        GC (reserved=7034970, committed=7034970)
                            (malloc=22618 #116)
                            (mmap: reserved=7012352, committed=7012352)

ParallelGC

java -Xmx2G -Xms2G -XX:+UseParallelGC -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version

NMT显示GC占用:111283672字节(106M)

❯ java -Xmx2G -Xms2G -XX:+UseParallelGC -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version
-XX:InitialHeapSize=2147483648 -XX:MaxHeapSize=2147483648 -XX:NativeMemoryTracking=summary -XX:+PrintCommandLineFlags -XX:+PrintNMTStatistics -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:+UnlockDiagnosticVMOptions -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC
VM settings:
    Min. Heap Size: 2.00G
    Max. Heap Size: 2.00G
    Using VM: OpenJDK 64-Bit Server VM

openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment Temurin-11.0.17+8 (build 11.0.17+8)
OpenJDK 64-Bit Server VM Temurin-11.0.17+8 (build 11.0.17+8, mixed mode)

Native Memory Tracking:

Total: reserved=3621375483, committed=2297957883
-                 Java Heap (reserved=2147483648, committed=2147483648)
                            (mmap: reserved=2147483648, committed=2147483648)

-                        GC (reserved=111283672, committed=111283672)
                            (malloc=32804312 #187)
                            (mmap: reserved=78479360, committed=78479360)

G1GC

java -Xmx2G -Xms2G -XX:+UseG1GC -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version

NMT显示GC占用:139719719字节(133M

❯ java -Xmx2G -Xms2G -XX:+UseG1GC -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version
-XX:G1ConcRefinementThreads=9 -XX:GCDrainStackTargetSize=64 -XX:InitialHeapSize=2147483648 -XX:MaxHeapSize=2147483648 -XX:NativeMemoryTracking=summary -XX:+PrintCommandLineFlags -XX:+PrintNMTStatistics -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:+UnlockDiagnosticVMOptions -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseG1GC
VM settings:
    Min. Heap Size: 2.00G
    Max. Heap Size: 2.00G
    Using VM: OpenJDK 64-Bit Server VM

openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment Temurin-11.0.17+8 (build 11.0.17+8)
OpenJDK 64-Bit Server VM Temurin-11.0.17+8 (build 11.0.17+8, mixed mode)

Native Memory Tracking:

Total: reserved=3658550668, committed=2335133068
-                 Java Heap (reserved=2147483648, committed=2147483648)
                            (mmap: reserved=2147483648, committed=2147483648)

-                        GC (reserved=139719719, committed=139719719)
                            (malloc=25916455 #14644)
                            (mmap: reserved=113803264, committed=113803264)

ShenandoahGC

ShenandoahGCRed Hat贡献给OpenJDK的垃圾回收器,OpenJDK 12+默认包含了该GCOpenJDK 11.0.9后中也引入了该GC

注意:所有OracleJDK,任何版本,都没有引入该GC

参考文档:https://wiki.openjdk.org/display/shenandoah/Main

java -Xmx2G -Xms2G -XX:+UseShenandoahGC -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version

NMT显示GC占用:已提交39276392字节(37M),保留72830824字节(69M

❯ java -Xmx2G -Xms2G -XX:+UseShenandoahGC -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version
-XX:InitialHeapSize=2147483648 -XX:MaxHeapSize=2147483648 -XX:NativeMemoryTracking=summary -XX:+PrintCommandLineFlags -XX:+PrintNMTStatistics -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:+UnlockDiagnosticVMOptions -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseShenandoahGC
VM settings:
    Min. Heap Size: 2.00G
    Max. Heap Size: 2.00G
    Using VM: OpenJDK 64-Bit Server VM

openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment Temurin-11.0.17+8 (build 11.0.17+8)
OpenJDK 64-Bit Server VM Temurin-11.0.17+8 (build 11.0.17+8, mixed mode)

Native Memory Tracking:

Total: reserved=3587868112, committed=2230896080
-                 Java Heap (reserved=2147483648, committed=2147483648)
                            (mmap: reserved=2147483648, committed=2147483648)

-                        GC (reserved=72830824, committed=39276392)
                            (malloc=5427048 #652)
                            (mmap: reserved=67403776, committed=33849344)

ZGC

java -Xmx2G -Xms2G -XX:+UseZGC -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version

NMT显示GC占用:已提交100813366字节(96M),保留8657193526字节(8256M

❯ java -Xmx2G -Xms2G -XX:+UseZGC -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version
[0.002s][warning][gc,jvmci] Setting EnableJVMCI to false as selected GC does not support JVMCI: z gc
-XX:+EnableJVMCIProduct -XX:InitialHeapSize=2147483648 -XX:MaxHeapSize=2147483648 -XX:MinHeapSize=2147483648 -XX:NativeMemoryTracking=summary -XX:+PrintCommandLineFlags -XX:+PrintNMTStatistics -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:ThreadPriorityPolicy=1 -XX:+UnlockDiagnosticVMOptions -XX:-UnlockExperimentalVMOptions -XX:+UseCompressedClassPointers -XX:-UseCompressedOops -XX:+UseZGC
VM settings:
    Min. Heap Size: 2.00G
    Max. Heap Size: 2.00G
    Using VM: OpenJDK 64-Bit Server VM

openjdk version "17.0.5" 2022-10-18 LTS
OpenJDK Runtime Environment GraalVM 22.3.0 (build 17.0.5+8-LTS)
OpenJDK 64-Bit Server VM GraalVM 22.3.0 (build 17.0.5+8-LTS, mixed mode)

Native Memory Tracking:

Total: reserved=115268659647, committed=2310629823
-                 Java Heap (reserved=103079215104, committed=2147483648)
                            (mmap: reserved=103079215104, committed=2147483648)

-                        GC (reserved=8657193526, committed=100813366)
                            (malloc=150070 #396)
                            (mmap: reserved=8657043456, committed=100663296)
Views: 578 · Posted: 2023-12-27

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

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

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


Today On History
Browsing Refresh