内存溢出及Jvm监控工具

时间:2022-08-21 03:42:39

内存泄露与内存溢出

内存溢出 out of memory,是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory。

内存泄露 memory leak,是指程序在申请内存后,无法释放已申请的内存空间。一次内存泄露危害可以忽略,但内存泄露堆积后果很严重,无论多少内存,迟早会被占光。
memory leak会最终会导致out of memory!

内存溢出分类

堆内存溢出
堆内存中存在大量对象,这些对象都有被引用,当所有对象占用空间达到堆内存的最大值,就会出现内存溢出OutOfMemory:Java heap space
永久代溢出
类的一些信息,如类名、访问修饰符、字段描述、方法描述等,所占空间大于永久代最大值,就会出现OutOfMemoryError:PermGen space

内存泄露检测手段

Jdk/bin目录下有很多检测工具
图形界面(需要更新测试机配置文件):
Jconsole
Jvisualvm
命令行工具:
Jstat –gcutil pid 1000 100
Jmap –histo pid | head -20
Jmap –heap pid

 1 [root@zhoucentos bin]# jmap -heap 2384   //打印java heap堆内存信息
2 Attaching to process ID 2384, please wait...
3 Debugger attached successfully.
4 Client compiler detected.
5 JVM version is 11.0-b15
6
7 using thread-local object allocation.
8 Mark Sweep Compact GC
9
10 Heap Configuration:
11 MinHeapFreeRatio = 40
12 MaxHeapFreeRatio = 70
13 MaxHeapSize = 268435456 (256.0MB)
14 NewSize = 131072000 (125.0MB)
15 MaxNewSize = 131072000 (125.0MB)
16 OldSize = 4194304 (4.0MB)
17 NewRatio = 12
18 SurvivorRatio = 8
19 PermSize = 12582912 (12.0MB)
20 MaxPermSize = 67108864 (64.0MB)
21
22 Heap Usage:
23 New Generation (Eden + 1 Survivor Space):
24 capacity = 117964800 (112.5MB)
25 used = 102007440 (97.28187561035156MB)
26 free = 15957360 (15.218124389648438MB)
27 86.4727783203125% used
28 Eden Space:
29 capacity = 104857600 (100.0MB)
30 used = 90339496 (86.15445709228516MB)
31 free = 14518104 (13.845542907714844MB)
32 86.15445709228516% used
33 From Space:
34 capacity = 13107200 (12.5MB)
35 used = 11667944 (11.127418518066406MB)
36 free = 1439256 (1.3725814819335938MB)
37 89.01934814453125% used
38 To Space:
39 capacity = 13107200 (12.5MB)
40 used = 0 (0.0MB)
41 free = 13107200 (12.5MB)
42 0.0% used
43 tenured generation:
44 capacity = 137363456 (131.0MB)
45 used = 40348976 (38.47978210449219MB)
46 free = 97014480 (92.52021789550781MB)
47 29.373879469078005% used
48 Perm Generation:
49 capacity = 12845056 (12.25MB)
50 used = 12656048 (12.069747924804688MB)
51 free = 189008 (0.1802520751953125MB)
52 98.52855448820154% used

配置tomcat jvm参数

内存溢出及Jvm监控工具

内存溢出及Jvm监控工具

编写接口脚本

内存溢出及Jvm监控工具

跑5分钟……

jvisualvm远程连接虚拟机监控

内存溢出及Jvm监控工具

可以看到堆内存到达了顶点,发生了垃圾回收却没有起作用。像这样暴力的,哈哈