一、问题现场
![](https://img-blog.csdnimg.cn/img_convert/f6141104f4e647773ba6d58bd02131a2.webp?x-oss-process=image/format,png)
1、这里只能看出是内存使用过高,无法知道是虚拟内存还是实际内存。
2、所以我们继续使用top命令,往下观察发现,是虚拟内存,而且是一个java进程就占用了4GB多,实际内存仅850MB。
![](https://img-blog.csdnimg.cn/img_convert/8a5073ffc87732f3c605ad2608a5ed98.webp?x-oss-process=image/format,png)
3、跟踪这个java进程
[root@localhost ~]# ps -ef | grep "29345"
root 24359 23706 0 11:29 pts/1 00:00:00 grep --color=auto 29345
root 29345 1 3 3月07 ? 14:38:18 java -Dfile.encoding=UTF-8 -Djava.rmi.server.hostname=192.168.53.29 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=3333 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Duser.timezone=Asia/Shanghai -Dspring.config.location=resources/application.yml -jar -Xms512m -Xmx1024m cwp2video-evolution.jar
这里有两个疑问:
- -Xms512m -Xmx1024m是在-jar前还是在-jar后,它对最大堆内存值没生效吗?
- java程序分配的虚拟内存是什么?
二、查看java进程的内存使用情况
1、jmap -heap 29345
Attaching to process ID 29345, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.301-b09
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 1073741824 (1024.0MB)
NewSize = 178782208 (170.5MB)
MaxNewSize = 357564416 (341.0MB)
OldSize = 358088704 (341.5MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 177209344 (169.0MB)
used = 173120624 (165.10069274902344MB)
free = 4088720 (3.8993072509765625MB)
97.69271760297245% used
From Space:
capacity = 4718592 (4.5MB)
used = 3614960 (3.4474945068359375MB)
free = 1103632 (1.0525054931640625MB)
76.61098904079861% used
To Space:
capacity = 4718592 (4.5MB)
used = 0 (0.0MB)
free = 4718592 (4.5MB)
0.0% used
PS Old Generation
capacity = 716177408 (683.0MB)
used = 283350976 (270.22454833984375MB)
free = 432826432 (412.77545166015625MB)
39.56435554024067% used
MaxHeapSize=1073741824 (1024.0MB), MetaspaceSize=21807104 (20.796875MB),而MaxMetaspaceSize=17592186044415 MB(相当于没有内存大小的限制了,需要注意!!)
内存大小 = NewSize+OldSize+MetaspaceSize
Tips: Java 程序由于自己维护堆的使用,导致调用 glibc 去管理内存的次数较少。更糟的是 Java 8 开始使用 metaspace 原空间取代永久代,而元空间是存放在操作系统本地内存中,那线程一多,每个线程都要使用一点元空间,每个线程都分配一个 arena,每个都64MB,就会导致巨大的虚拟地址被分配。
2、pmap -x 29345
![](https://img-blog.csdnimg.cn/img_convert/94a129334adf67f65d40f4e4c101a245.webp?x-oss-process=image/format,png)
中间省略许多的截图,太多了,不一一展示了。。。
![](https://img-blog.csdnimg.cn/img_convert/241f367063484032c09aa7a4dca66eef.webp?x-oss-process=image/format,png)
最后一行是total汇总信息,也能看出总的虚拟内存是
VIRT高是因为分配了太多地址空间导致。使用pmap命令查看到底哪些占用得多虚拟内存。。。
建议控制VIRT的使用,设置环境变量MALLOC_ARENA_MAX,推荐值为CPU的核数。
export MALLOC_ARENA_MAX=4
3、查看另外一个java进程的内存占用
[root@localhost cwp2video]# pmap -x 31979
31979: java -Dfile.encoding=UTF-8 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=3334 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Duser.timezone=Asia/Shanghai -jar -Xms256m -Xmx512m adb-screen.jar -redisHost=127.0.0.1 -redisPort=6379 -endpoint=http://192.168.8.18:9000 -aKey=chenyz -sKey=123123123123 -bucket=images -debug=true -Djava.rmi.server.hostname=192.168.53.29
Address Kbytes RSS Dirty Mode Mapping
00000000e0000000 175104 30728 30728 rw--- [ anon ]
00000000eab00000 174592 0 0 ----- [ anon ]
00000000f5580000 87040 2068 2068 rw--- [ anon ]
00000000faa80000 87552 0 0 ----- [ anon ]
0000000100000000 2560 2048 2048 rw--- [ anon ]
0000000100280000 1046016 0 0 ----- [ anon ]
0000562a6f949000 4 0 0 r-x-- java
0000562a6fb49000 4 0 0 r---- java
0000562a6fb4a000 4 0 0 rw--- java
0000562a6fed7000 132 0 0 rw--- [ anon ]
00007f1850000000 132 8 8 rw--- [ anon ]
00007f1850021000 65404 0 0 ----- [ anon ]
00007f1854000000 132 0 0 rw--- [ anon ]
00007f1854021000 65404 0 0 ----- [ anon ]
00007f1858000000 136 8 8 rw--- [ anon ]
00007f1858022000 65400 0 0 ----- [ anon ]
00007f185c000000 132 0 0 rw--- [ anon ]
00007f185c021000 65404 0 0 ----- [ anon ]
00007f1860000000 132 0 0 rw--- [ anon ]
00007f1860021000 65404 0 0 ----- [ anon ]
00007f1864000000 260 4 4 rw--- [ anon ]
00007f1864041000 65276 0 0 ----- [ anon ]
00007f1868000000 6652 2048 2048 rw--- [ anon ]
00007f186867f000 58884 0 0 ----- [ anon ]
00007f186c000000 132 0 0 rw--- [ anon ]
00007f186c021000 65404 0 0 ----- [ anon ]
00007f1870000000 132 0 0 rw--- [ anon ]
00007f1870021000 65404 0 0 ----- [ anon ]
00007f1874000000 188 0 0 rw--- [ anon ]
00007f187402f000 65348 0 0 ----- [ anon ]
00007f1878000000 132 0 0 rw--- [ anon ]
00007f1878021000 65404 0 0 ----- [ anon ]
00007f187c000000 2936 0 0 rw--- [ anon ]
00007f187c2de000 62600 0 0 ----- [ anon ]
00007f1880000000 132 4 4 rw--- [ anon ]
00007f1880021000 65404 0 0 ----- [ anon ]
00007f1884000000 560 0 0 rw--- [ anon ]
00007f188408c000 64976 0 0 ----- [ anon ]
00007f1888000000 132 0 0 rw--- [ anon ]
00007f1888021000 65404 0 0 ----- [ anon ]
00007f188c000000 132 0 0 rw--- [ anon ]
00007f188c021000 65404 0 0 ----- [ anon ]
00007f1890000000 132 0 0 rw--- [ anon ]
00007f1890021000 65404 0 0 ----- [ anon ]
00007f1894000000 132 0 0 rw--- [ anon ]
00007f1894021000 65404 0 0 ----- [ anon ]
00007f1898000000 7028 0 0 rw--- [ anon ]
00007f18986dd000 58508 0 0 ----- [ anon ]
00007f189c000000 132 0 0 rw--- [ anon ]
00007f189c021000 65404 0 0 ----- [ anon ]
00007f18a0000000 31544 0 0 rw--- [ anon ]
00007f18a1ece000 33992 0 0 ----- [ anon ]
00007f18a4000000 25452 0 0 rw--- [ anon ]
00007f18a58db000 40084 0 0 ----- [ anon ]
00007f18a8000000 132 0 0 rw--- [ anon ]
00007f18a8021000 65404 0 0 ----- [ anon ]
00007f18adabe000 103688 0 0 r---- locale-archive
00007f18b4000000 132 0 0 rw--- [ anon ]
00007f18b4021000 65404 0 0 ----- [ anon ]
00007f18b8000000 132 0 0 rw--- [ anon ]
00007f18b8021000 65404 0 0 ----- [ anon ]
00007f18bc000000 132 0 0 rw--- [ anon ]
00007f18bc021000 65404 0 0 ----- [ anon ]
00007f18c0000000 776 4 4 rw--- [ anon ]
00007f18c00c2000 64760 0 0 ----- [ anon ]
00007f18c4000000 132 0 0 rw--- [ anon ]
00007f18c4021000 65404 0 0 ----- [ anon ]
00007f18cb5fa000 12 0 0 ----- [ anon ]
00007f18cb5fd000 1016 4 4 rw--- [ anon ]
00007f18cb6fb000 12 0 0 ----- [ anon ]
00007f18cb6fe000 1784 0 0 rw--- [ anon ]
00007f18cb8bc000 1292 0 0 ----- [ anon ]
00007f18cb9ff000 1016 0 0 rw--- [ anon ]
00007f18cbafd000 12 0 0 ----- [ anon ]
00007f18cbb00000 1016 0 0 rw--- [ anon ]
00007f18cbbfe000 12 0 0 ----- [ anon ]
00007f18cbc01000 1016 0 0 rw--- [ anon ]
00007f18cbcff000 12 0 0 ----- [ anon ]
00007f18cbd02000 3064 0 0 rw--- [ anon ]
00007f18cc000000 132 0 0 rw--- [ anon ]
00007f18cc021000 65404 0 0 ----- [ anon ]
00007f18d0022000 12 0 0 ----- [ anon ]
00007f18d0025000 216 0 0 rw--- [ anon ]
00007f18d005b000 12 0 0 ----- [ anon ]
00007f18d005e000 3064 4 4 rw--- [ anon ]
00007f18d035c000 84 0 0 r-x-- libgcc_s-4.8.5-20150702.so.1
00007f18d0371000 2044 0 0 ----- libgcc_s-4.8.5-20150702.so.1
00007f18d0570000 4 0 0 r---- libgcc_s-4.8.5-20150702.so.1
00007f18d0571000 4 0 0 rw--- libgcc_s-4.8.5-20150702.so.1
00007f18d0582000 224 0 0 r-x-- libsunec.so
00007f18d05ba000 2044 0 0 ----- libsunec.so
00007f18d07b9000 20 0 0 r---- libsunec.so
00007f18d07be000 8 0 0 rw--- libsunec.so
00007f18d07c0000 2048 4 4 rw--- [ anon ]
00007f18d09c0000 2048 36 36 rw--- [ anon ]
00007f18d0bc0000 12 0 0 ----- [ anon ]
00007f18d0bc3000 1016 0 0 rw--- [ anon ]
00007f18d0cc1000 12 0 0 ----- [ anon ]
00007f18d0cc4000 3064 8 8 rw--- [ anon ]
00007f18d0fc2000 12 0 0 ----- [ anon ]
00007f18d0fc5000 1016 0 0 rw--- [ anon ]
00007f18d10c3000 12 0 0 ----- [ anon ]
00007f18d10c6000 216 0 0 rw--- [ anon ]
00007f18d10fc000 12 0 0 ----- [ anon ]
00007f18d10ff000 1016 0 0 rw--- [ anon ]
00007f18d11fd000 12 0 0 ----- [ anon ]
00007f18d1200000 1016 0 0 rw--- [ anon ]
00007f18d12fe000 12 0 0 ----- [ anon ]
00007f18d1301000 1016 0 0 rw--- [ anon ]
00007f18d13ff000 12 0 0 ----- [ anon ]
00007f18d1402000 3064 60 60 rw--- [ anon ]
00007f18d1700000 4 0 0 ----- [ anon ]
00007f18d1701000 1024 8 8 rw--- [ anon ]
00007f18d1801000 12 0 0 ----- [ anon ]
00007f18d1804000 1016 0 0 rw--- [ anon ]
00007f18d1902000 12 0 0 ----- [ anon ]
00007f18d1905000 1016 0 0 rw--- [ anon ]
00007f18d1a03000 12 0 0 ----- [ anon ]
00007f18d1a06000 1016 0 0 rw--- [ anon ]
00007f18d1b04000 716 0 0 r--s- adb-screen.jar
00007f18d1bb7000 88 0 0 r-x-- libnet.so
00007f18d1bcd000 2044 0 0 ----- libnet.so
00007f18d1dcc000 4 0 0 r---- libnet.so
00007f18d1dcd000 4 0 0 rw--- libnet.so
00007f18d1dce000 68 0 0 r-x-- libnio.so
00007f18d1ddf000 2044 0 0 ----- libnio.so
00007f18d1fde000 4 0 0 r---- libnio.so
00007f18d1fdf000 4 0 0 rw--- libnio.so
00007f18d1fe0000 36 0 0 r-x-- libmanagement.so
00007f18d1fe9000 2044 0 0 ----- libmanagement.so
00007f18d21e8000 4 0 0 r---- libmanagement.so
00007f18d21e9000 4 0 0 rw--- libmanagement.so
00007f18d21ea000 12 0 0 ----- [ anon ]
00007f18d21ed000 1016 0 0 rw--- [ anon ]
00007f18d22eb000 16 0 0 ----- [ anon ]
00007f18d22ef000 1012 8 8 rw--- [ anon ]
00007f18d23ec000 16 0 0 ----- [ anon ]
00007f18d23f0000 1012 8 8 rw--- [ anon ]
00007f18d24ed000 16 0 0 ----- [ anon ]
00007f18d24f1000 1012 8 8 rw--- [ anon ]
00007f18d25ee000 12 0 0 ----- [ anon ]
00007f18d25f1000 1016 0 0 rw--- [ anon ]
00007f18d26ef000 12 0 0 ----- [ anon ]
00007f18d26f2000 1016 0 0 rw--- [ anon ]
00007f18d27f0000 12 0 0 ----- [ anon ]
00007f18d27f3000 1016 0 0 rw--- [ anon ]
00007f18d28f1000 4 0 0 ----- [ anon ]
00007f18d28f2000 23608 8 8 rw--- [ anon ]
00007f18d4000000 132 0 0 rw--- [ anon ]
00007f18d4021000 65404 0 0 ----- [ anon ]
00007f18d8000000 16384 0 0 rw--- [ anon ]
00007f18d9000000 15744 10272 10272 rwx-- [ anon ]
00007f18d9f60000 230016 0 0 ----- [ anon ]
00007f18e8000000 6552 4096 4096 rw--- [ anon ]
00007f18e8666000 58984 0 0 ----- [ anon ]
00007f18ec008000 24 0 0 r--s- sunjce_provider.jar
00007f18ec00e000 84 0 0 r--s- resources.jar
00007f18ec023000 28 0 0 r--s- gconv-modules.cache
00007f18ec02a000 140 0 0 r--s- cldrdata.jar
00007f18ec04d000 1348 0 0 rw--- [ anon ]
00007f18ec19e000 1912 0 0 r--s- rt.jar
00007f18ec37c000 1860 8 8 rw--- [ anon ]
00007f18ec54d000 4 0 0 ----- [ anon ]
00007f18ec54e000 1024 0 0 rw--- [ anon ]
00007f18ec64e000 4 0 0 ----- [ anon ]
00007f18ec64f000 1024 0 0 rw--- [ anon ]
00007f18ec74f000 4 0 0 ----- [ anon ]
00007f18ec750000 1024 0 0 rw--- [ anon ]
00007f18ec850000 4 0 0 ----- [ anon ]
00007f18ec851000 1368 4 4 rw--- [ anon ]
00007f18ec9a7000 336 0 0 ----- [ anon ]
00007f18ec9fb000 176 4 4 rw--- [ anon ]
00007f18eca27000 168 0 0 ----- [ anon ]
00007f18eca51000 252 8 8 rw--- [ anon ]
00007f18eca90000 3592 0 0 ----- [ anon ]
00007f18ece12000 108 0 0 r-x-- libzip.so
00007f18ece2d000 2044 0 0 ----- libzip.so
00007f18ed02c000 4 0 0 r---- libzip.so
00007f18ed02d000 4 0 0 rw--- libzip.so
00007f18ed02e000 48 0 0 r-x-- libnss_files-2.17.so
00007f18ed03a000 2044 0 0 ----- libnss_files-2.17.so
00007f18ed239000 4 0 0 r---- libnss_files-2.17.so
00007f18ed23a000 4 0 0 rw--- libnss_files-2.17.so
00007f18ed23b000 24 0 0 rw--- [ anon ]
00007f18ed241000 176 0 0 r-x-- libjava.so
00007f18ed26d000 2048 0 0 ----- libjava.so
00007f18ed46d000 4 0 0 r---- libjava.so
00007f18ed46e000 8 0 0 rw--- libjava.so
00007f18ed470000 52 0 0 r-x-- libverify.so
00007f18ed47d000 2044 0 0 ----- libverify.so
00007f18ed67c000 8 0 0 r---- libverify.so
00007f18ed67e000 4 0 0 rw--- libverify.so
00007f18ed67f000 28 0 0 r-x-- librt-2.17.so
00007f18ed686000 2044 0 0 ----- librt-2.17.so
00007f18ed885000 4 0 0 r---- librt-2.17.so
00007f18ed886000 4 0 0 rw--- librt-2.17.so
00007f18ed887000 1028 8 0 r-x-- libm-2.17.so
00007f18ed988000 2044 0 0 ----- libm-2.17.so
00007f18edb87000 4 0 0 r---- libm-2.17.so
00007f18edb88000 4 0 0 rw--- libm-2.17.so
00007f18edb89000 13272 260 0 r-x-- libjvm.so
00007f18ee87f000 2044 0 0 ----- libjvm.so
00007f18eea7e000 600 28 28 r---- libjvm.so
00007f18eeb14000 196 20 20 rw--- libjvm.so
00007f18eeb45000 236 64 64 rw--- [ anon ]
00007f18eeb80000 1808 100 0 r-x-- libc-2.17.so
00007f18eed44000 2044 0 0 ----- libc-2.17.so
00007f18eef43000 16 4 4 r---- libc-2.17.so
00007f18eef47000 8 4 4 rw--- libc-2.17.so
00007f18eef49000 20 0 0 rw--- [ anon ]
00007f18eef4e000 8 0 0 r-x-- libdl-2.17.so
00007f18eef50000 2048 0 0 ----- libdl-2.17.so
00007f18ef150000 4 0 0 r---- libdl-2.17.so
00007f18ef151000 4 0 0 rw--- libdl-2.17.so
00007f18ef152000 92 0 0 r-x-- libjli.so
00007f18ef169000 2044 0 0 ----- libjli.so
00007f18ef368000 4 0 0 r---- libjli.so
00007f18ef369000 4 0 0 rw--- libjli.so
00007f18ef36a000 92 24 0 r-x-- libpthread-2.17.so
00007f18ef381000 2044 0 0 ----- libpthread-2.17.so
00007f18ef580000 4 0 0 r---- libpthread-2.17.so
00007f18ef581000 4 4 4 rw--- libpthread-2.17.so
00007f18ef582000 16 4 4 rw--- [ anon ]
00007f18ef586000 136 0 0 r-x-- ld-2.17.so
00007f18ef5ab000 80 0 0 r---- libc.mo
00007f18ef5bf000 44 0 0 r--s- localedata.jar
00007f18ef5ca000 72 0 0 r--s- jsse.jar
00007f18ef5dc000 384 0 0 rw--- [ anon ]
00007f18ef63c000 340 0 0 ----- [ anon ]
00007f18ef691000 16 0 0 ----- [ anon ]
00007f18ef695000 1028 0 0 rw--- [ anon ]
00007f18ef797000 8 0 0 r--s- sunec.jar
00007f18ef799000 12 0 0 r--s- jce.jar
00007f18ef79c000 32 8 8 rw-s- 31979
00007f18ef7a4000 4 4 4 rw--- [ anon ]
00007f18ef7a5000 4 0 0 r---- [ anon ]
00007f18ef7a6000 4 0 0 rw--- [ anon ]
00007f18ef7a7000 4 0 0 r---- ld-2.17.so
00007f18ef7a8000 4 0 0 rw--- ld-2.17.so
00007f18ef7a9000 4 0 0 rw--- [ anon ]
00007ffc91bb4000 132 4 0 rw--- [ stack ]
00007ffc91bee000 8 4 0 r-x-- [ anon ]
ffffffffff600000 4 0 0 r-x-- [ anon ]
---------------- ------- ------- -------
total kB 4103152 52008 51608
这里有大量的占用内存大小为65404KB。