MyCAT报java.lang.OutOfMemoryError: Java heap space

时间:2022-06-09 12:41:30

早上同事反映,mycat又假死了,估计还是内存溢出,查看了一下错误日志。

INFO   | jvm     | // :: | java.lang.OutOfMemoryError: Java heap space
INFO | jvm | // :: | Exception in thread "Timer67" java.lang.OutOfMemoryError: Java heap space
INFO | jvm | // :: | Exception in thread "Timer68" java.lang.OutOfMemoryError: Java heap space
INFO | jvm | // :: | Exception in thread "Timer69" java.lang.OutOfMemoryError: Java heap space
INFO | jvm | // :: | Exception in thread "Timer70" java.lang.OutOfMemoryError: Java heap space
INFO | jvm | // :: | Exception in thread "Timer71" java.lang.OutOfMemoryError: Java heap space
INFO | jvm | // :: | Exception in thread "Timer72" java.lang.OutOfMemoryError: Java heap space
INFO | jvm | // :: | Exception in thread "Timer74" java.lang.OutOfMemoryError: Java heap space
INFO | jvm | // :: | Exception in thread "Timer73" java.lang.OutOfMemoryError: Java heap space

堆内存溢出,以为是内存不足,但与前几天的报错又不一样

INFO   | jvm     | // :: | Exception in thread "$_MyCatServer" java.lang.OutOfMemoryError: Direct buffer memory
INFO | jvm | // :: | at java.nio.Bits.reserveMemory(Bits.java:)
INFO | jvm | // :: | at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:)
INFO | jvm | // :: | at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:)

而且,通过查看wrapper.conf,发现MaxDirectMemorySize已经设置为10G了,刚开始一直按默认2G来,偶尔会出现内存溢出。

wrapper.java.additional.=-XX:MaxDirectMemorySize=10G

后来,一高人指点,通过jmap -heap pid命令查看,发现MaxHeapSize=16MB

Heap Configuration:
MinHeapFreeRatio =
MaxHeapFreeRatio =
MaxHeapSize = (.0MB)
NewSize = (.25MB)
MaxNewSize = MB
OldSize = (.1875MB)
NewRatio =
SurvivorRatio =
PermSize = (.75MB)
MaxPermSize = (.0MB)
G1HeapRegionSize = (.0MB)

不科学啊,wrapper.conf中有关堆内存的设置没这么小啊

wrapper.java.additional.=-Xmx4G
wrapper.java.additional.=-Xms1G # Initial Java Heap Size (in MB)
wrapper.java.initmemory=4G # Maximum Java Heap Size (in MB)
wrapper.java.maxmemory=15G

最后才发现,问题出在wrapper.java.initmemory=4G和wrapper.java.maxmemory=15G上了。

原来,这两个参数直接设置数值就可以了,因为它的单位已经已经指明了,为MB。

如果设置为15G,jvm会直接屏蔽掉后面的G,使用15,然后MaxHeapSize将会被设置为16MB(后来测试了一下,如果设置为17,MaxHeapSize则会被设置为18MB,至于为什么每次都分配为偶数,这个就不太清楚了)。

后来测试了一下,得出以下结论:

1. wrapper.java.maxmemory参数会覆盖wrapper.java.additional.10参数。

2. 即便wrapper.java.maxmemory的值超过了4096,MaxHeapSize最大为4096MB。

3. 如果堆大小要超过4096MB,可设置wrapper.java.additional.10,同时注释掉wrapper.java.maxmemory。

建议,只使用wrapper.java.additional.10=-Xmx4G和wrapper.java.additional.11=-Xms1G配置堆内存的大小。

总结:

mycat的启动方式有两种

1. mycat/bin/mycat start

这种方式会使用mycat/conf/wrapper.conf作为配置文件

通过 sh -x mycat start即可发现

+ eval '"/usr/local/mycat/bin/./wrapper-linux-x86-64"' '"/usr/local/mycat/conf/wrapper.conf"' wrapper.syslog.ident=mycat 'wrapper.pidfile="/usr/local/mycat/logs/mycat.pid"' wrapper.daemonize=TRUE 'wrapper.lockfile="/var/lock/subsys/mycat"'
++ /usr/local/mycat/bin/./wrapper-linux-x86- /usr/local/mycat/conf/wrapper.conf wrapper.syslog.ident=mycat wrapper.pidfile=/usr/local/mycat/logs/mycat.pid wrapper.daemonize=TRUE wrapper.lockfile=/var/lock/subsys/myca

2. mycat/bin/startup_nowrap.sh

这个脚本里面同样有关于jvm的配置

JAVA_OPTS="-server -Xms2G -Xmx2G -XX:MaxPermSize=64M  -XX:+AggressiveOpts -XX:MaxDirectMemorySize=2G"
# ./startup_nowrap.sh
"/usr/local/jdk1.7.0_79/bin/java" -DMYCAT_HOME="/usr/local/mycat" -classpath "/usr/local/mycat/conf:/usr/local/mycat/lib/classes:/usr/local/mycat/lib/curator-client-2.9.0.jar:/usr/local/mycat/lib/curator-framework-2.9.0.jar:/usr/local/mycat/lib/dom4j-1.6.1.jar:/usr/local/mycat/lib/druid-1.0.14.jar:/usr/local/mycat/lib/ehcache-core-2.6.11.jar:/usr/local/mycat/lib/fastjson-1.2.7.jar:/usr/local/mycat/lib/guava-18.0.jar:/usr/local/mycat/lib/jline-0.9.94.jar:/usr/local/mycat/lib/json-20151123.jar:/usr/local/mycat/lib/leveldb-0.7.jar:/usr/local/mycat/lib/leveldb-api-0.7.jar:/usr/local/mycat/lib/log4j-1.2.17.jar:/usr/local/mycat/lib/mapdb-1.0.7.jar:/usr/local/mycat/lib/mongo-java-driver-2.11.4.jar:/usr/local/mycat/lib/Mycat-server-1.5-GA.jar:/usr/local/mycat/lib/netty-3.7.0.Final.jar:/usr/local/mycat/lib/ojdbc14-10.2.0.3.0.jar:/usr/local/mycat/lib/sequoiadb-java-driver-1.0-20150615.070208-1.jar:/usr/local/mycat/lib/slf4j-api-1.7.12.jar:/usr/local/mycat/lib/slf4j-log4j12-1.7.12.jar:/usr/local/mycat/lib/snakeyaml-1.16.jar:/usr/local/mycat/lib/univocity-parsers-1.5.4.jar:/usr/local/mycat/lib/wrapper.jar:/usr/local/mycat/lib/xml-apis-1.0.b2.jar:/usr/local/mycat/lib/zookeeper-3.4.6.jar" -server -Xms2G -Xmx2G -XX:MaxPermSize=64M -XX:+AggressiveOpts -XX:MaxDirectMemorySize=2G org.opencloudb.MycatStartup >> "/usr/local/mycat/logs/console.log" >& &

后续-20160617

--------------------------------------------------------

通过mycat start启动失败,wrapper.log有如下报错信息:

STATUS | wrapper  | // :: | Launching a JVM...
ERROR | wrapper | // :: | Startup failed: Timed out waiting for a signal from the JVM.
ERROR | wrapper | // :: | JVM did not exit on request, terminated
INFO | wrapper | // :: | JVM exited on its own while waiting to kill the application.
STATUS | wrapper | // :: | JVM exited in response to signal SIGKILL ().

后来通过startup_nowrap.sh,发现可以启动成功。

在网上找了一把,可修改conf/wrapper.conf中设置如下参数:

#设置超时时间为2小时wrapper.startup.timeout=7200

#设置心跳超时时间为1小时wrapper.ping.timeout=3600

重新通过mycat start启动,启动成功~