tomcat Java heap space 内存溢出

Posted by Yancy on 2017-08-18

tomcat Java heap space 内存溢出

confluence服务器早上BI那边反应访问wiki出现504 Gateway time-out 之前定位到问题是服务器CPU负载过高 之前服务器配置1核1G的配置 导致用户请求超时,后面服务器升级到2核4G内存,默认wiki没有内存优化,导致这次访问出现内存溢出的报错。

内存溢出日志报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2017-08-18 16:30:07,181 INFO [main] [com.atlassian.confluence.lifecycle] contextInitialized Starting Confluence 3.4.5 (build #2035)
2017-08-18 16:30:10,297 INFO [main] [atlassian.plugin.manager.DefaultPluginManager] init Initialising the plugin system
2017-08-18 16:30:10,519 INFO [main] [atlassian.plugin.manager.DefaultPluginManager] init Plugin system started in 0:00:00.219
Aug 18, 2017 4:30:51 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Aug 18, 2017 4:30:51 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 48320 ms
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "Timer-0"
Exception in thread "DefaultQuartzScheduler_QuartzSchedulerThread" java.lang.OutOfMemoryError: Java heap space
Exception in thread "com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2" java.lang.OutOfMemoryError: Java heap space
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "ContainerBackgroundProcessor[StandardEngine[Standalone]]"
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1"
Exception in thread "com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "DefaultQuartzScheduler_Worker-1" java.lang.OutOfMemoryError: Java heap space
Exception in thread "DefaultQuartzScheduler_Worker-3" java.lang.OutOfMemoryError: Java heap space
Exception in thread "DefaultQuartzScheduler_Worker-2" java.lang.OutOfMemoryError: Java heap space
Exception in thread "http-8080-2" java.lang.OutOfMemoryError: Java heap space

第一查看现在Java应用进程运行状态是否优化过,运行的内存大小多少。

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
48
49
50
51
[root@wiki-bi logs]# jmap -heap 4299
Attaching to process ID 4299, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 23.25-b01
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 536870912 (512.0MB)
NewSize = 1310720 (1.25MB)
MaxNewSize = 17592186044415 MB
OldSize = 5439488 (5.1875MB)
NewRatio = 2
SurvivorRatio = 8
PermSize = 21757952 (20.75MB)
MaxPermSize = 268435456 (256.0MB)
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 71958528 (68.625MB)
used = 71943440 (68.61061096191406MB)
free = 15088 (0.0143890380859375MB)
99.97903236708788% used
From Space:
capacity = 52166656 (49.75MB)
used = 0 (0.0MB)
free = 52166656 (49.75MB)
0.0% used
To Space:
capacity = 53477376 (51.0MB)
used = 0 (0.0MB)
free = 53477376 (51.0MB)
0.0% used
PS Old Generation
capacity = 357957632 (341.375MB)
used = 357957344 (341.3747253417969MB)
free = 288 (2.74658203125E-4MB)
99.9999195435509% used
PS Perm Generation
capacity = 90898432 (86.6875MB)
used = 90605744 (86.40837097167969MB)
free = 292688 (0.2791290283203125MB)
99.67800544678262% used
43519 interned Strings occupying 4740128 bytes.

这里可以看到MaxHeapSize 等参数默认512MB,这里优化内存配置后

1
2
3
4
vim /data/wiki-bi/bin/setenv.sh
JAVA_OPTS="-Xms2g -Xmx2g -Xss256k -XX:PermSize=256M -XX:MaxPermSize=256m -Duser.timezone=Asia/Shanghai $JAVA_OPTS -Djava.awt.headless=true "
export JAVA_OPTS

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
48
49
50
51
[root@wiki-bi bin]# jmap -heap 25727
Attaching to process ID 25727, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 23.25-b01
using thread-local object allocation.
Parallel GC with 4 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 2147483648 (2048.0MB)
NewSize = 1310720 (1.25MB)
MaxNewSize = 17592186044415 MB
OldSize = 5439488 (5.1875MB)
NewRatio = 2
SurvivorRatio = 8
PermSize = 268435456 (256.0MB)
MaxPermSize = 268435456 (256.0MB)
G1HeapRegionSize = 0 (0.0MB)
Heap Usage:
PS Young Generation
Eden Space:
capacity = 536870912 (512.0MB)
used = 448110856 (427.35181427001953MB)
free = 88760056 (84.64818572998047MB)
83.46715122461319% used
From Space:
capacity = 89456640 (85.3125MB)
used = 0 (0.0MB)
free = 89456640 (85.3125MB)
0.0% used
To Space:
capacity = 89456640 (85.3125MB)
used = 0 (0.0MB)
free = 89456640 (85.3125MB)
0.0% used
PS Old Generation
capacity = 1431699456 (1365.375MB)
used = 0 (0.0MB)
free = 1431699456 (1365.375MB)
0.0% used
PS Perm Generation
capacity = 268435456 (256.0MB)
used = 35112728 (33.486106872558594MB)
free = 233322728 (222.5138931274414MB)
13.0805104970932% used
15180 interned Strings occupying 1629600 bytes.