MyCAT报java.lang.OutOfMemoryError: Java heap space
早上同事反映,mycat又假死了,估计还是内存溢出,查看了一下错误日志。
INFO | jvm 1 | 2016/03/25 07:09:06 | java.lang.OutOfMemoryError: Java heap space
INFO | jvm 1 | 2016/03/25 07:09:06 | Exception in thread "Timer67" java.lang.OutOfMemoryError: Java heap space
INFO | jvm 1 | 2016/03/25 07:09:06 | Exception in thread "Timer68" java.lang.OutOfMemoryError: Java heap space
INFO | jvm 1 | 2016/03/25 07:09:06 | Exception in thread "Timer69" java.lang.OutOfMemoryError: Java heap space
INFO | jvm 1 | 2016/03/25 07:09:06 | Exception in thread "Timer70" java.lang.OutOfMemoryError: Java heap space
INFO | jvm 1 | 2016/03/25 07:09:06 | Exception in thread "Timer71" java.lang.OutOfMemoryError: Java heap space
INFO | jvm 1 | 2016/03/25 07:09:06 | Exception in thread "Timer72" java.lang.OutOfMemoryError: Java heap space
INFO | jvm 1 | 2016/03/25 07:09:07 | Exception in thread "Timer74" java.lang.OutOfMemoryError: Java heap space
INFO | jvm 1 | 2016/03/25 07:09:07 | Exception in thread "Timer73" java.lang.OutOfMemoryError: Java heap space
堆内存溢出,以为是内存不足,但与前几天的报错又不一样
INFO | jvm 1 | 2016/03/23 15:38:35 | Exception in thread "$_MyCatServer" java.lang.OutOfMemoryError: Direct buffer memory
INFO | jvm 1 | 2016/03/23 15:38:35 | at java.nio.Bits.reserveMemory(Bits.java:658)
INFO | jvm 1 | 2016/03/23 15:38:35 | at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
INFO | jvm 1 | 2016/03/23 15:38:35 | at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:306)
而且,通过查看wrapper.conf,发现MaxDirectMemorySize已经设置为10G了,刚开始一直按默认2G来,偶尔会出现内存溢出。
wrapper.java.additional.5=-XX:MaxDirectMemorySize=10G
后来,一高人指点,通过jmap -heap pid命令查看,发现MaxHeapSize=16MB
Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 16777216 (16.0MB)
NewSize = 1310720 (1.25MB)
MaxNewSize = 17592186044415 MB
OldSize = 5439488 (5.1875MB)
NewRatio = 2
SurvivorRatio = 8
PermSize = 21757952 (20.75MB)
MaxPermSize = 536870912 (512.0MB)
G1HeapRegionSize = 0 (0.0MB)
不科学啊,wrapper.conf中有关堆内存的设置没这么小啊
wrapper.java.additional.10=-Xmx4G
wrapper.java.additional.11=-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.maxmemory,同时注释掉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-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/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" 2>&1 &