El Psy Congroo

查看JAVA应用内存占用

物理内存占用

Committed & Reserved & RSS

Committed

Address ranges that have been mapped with something other than PROT_NONE. They may or may not be backed by physical or swap due to lazy allocation and paging.
已分配的内存,但不一定占用物理内存或swap

Reserved

The total address range that has been pre-mapped via mmap for a particular memory pool.
Committed的区别在于Reserved包含PROT_NONE一定不占用物理内存)的映射

RSS

Resident set size, the non-swapped physical memory that a task has used (in kiloBytes)
实际占用的物理内存(不包括已Committed但未分配的,以及swap-out的)
参考:RSS != JVM committed

1
2
$ps axo pid,rss | grep 25921 | cut -d' ' -f2
1039472

内存占用分解

Heap/Metaspace
1
2
3
$jstat -gc 25921
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
0.0 16384.0 0.0 16384.0 262144.0 196608.0 245760.0 210365.6 155596.0 147302.3 19660.0 18220.8 185 7.066 0 0.000 7.066

实际占用

Heap = SC + EC + OC = 16384 + 262144 + 245760 = 524288
Metaspace = 155596

栈空间
  • 默认 ThreadStackSize

    1
    2
    3
    $java -XX:+PrintFlagsFinal -version | grep ThreadStackSize
    intx ThreadStackSize = 1024 {pd product}
    intx VMThreadStackSize = 1024 {pd product}
  • 线程数量

    1
    2
    $jstack 25921 | grep -oP '(?<=java.lang.Thread.State: )\w+' | wc -l
    423
  • 实际占用

    ThreadStackSize 线程数量 = 1024 423 = 433152
    注意:这个实际上是Reserved内存,NMT报告为了CommittedJDK-8191369

  • 其他

    • 直接申请的堆外内存,如DirectBuffer
    • Codecache

Native Memory Tracking

JDK1.8自带的内存统计工具,通过-XX:NativeMemoryTracking=summary启用,启用会带来5~10%的性能开销

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
$ jcmd 25921 VM.native_memory summary
Native Memory Tracking:
Total: reserved=2739368KB, committed=1534736KB
- Java Heap (reserved=524288KB, committed=524288KB) <-- 启用了-XX:+AlwaysPreTouch,因此reserved和committed相同
(mmap: reserved=524288KB, committed=524288KB)
- Class (reserved=1190115KB, committed=159535KB)
(classes #26978)
(malloc=4323KB #47915)
(mmap: reserved=1185792KB, committed=155212KB)
- Thread (reserved=457839KB, committed=457839KB)
(thread #445)
(stack: reserved=456324KB, committed=456324KB)
(malloc=578KB #2232)
(arena=937KB #889)
- Code (reserved=265179KB, committed=92151KB)
(malloc=15579KB #21963)
(mmap: reserved=249600KB, committed=76572KB)
- GC (reserved=70422KB, committed=70422KB)
(malloc=18198KB #22462)
(mmap: reserved=52224KB, committed=52224KB)
- Compiler (reserved=895KB, committed=895KB)
(malloc=764KB #1353)
(arena=131KB #3)
- Internal (reserved=188191KB, committed=188191KB)
(malloc=188159KB #40270)
(mmap: reserved=32KB, committed=32KB)
- Symbol (reserved=34117KB, committed=34117KB)
(malloc=30944KB #312557)
(arena=3173KB #1)
- Native Memory Tracking (reserved=7090KB, committed=7090KB)
(malloc=53KB #595)
(tracking overhead=7037KB)
- Arena Chunk (reserved=209KB, committed=209KB)
(malloc=209KB)
- Unknown (reserved=1024KB, committed=0KB)
(mmap: reserved=1024KB, committed=0KB)