最近使用Thrift TThreadedSelectorServer服务方式,运行一段时间就会抛OutOfMemoryError: Direct buffer memory异常;
java.lang.OutOfMemoryError: Direct buffer memory
at java.nio.Bits.reserveMemory(Bits.java:658) ~[na:1.7.0_67]
at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123) ~[na:1.7.0_67]
at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:306) ~[na:1.7.0_67]
at sun.nio.ch.Util.getTemporaryDirectBuffer(Util.java:174) ~[na:1.7.0_67]
at sun.nio.ch.IOUtil.read(IOUtil.java:195) ~[na:1.7.0_67]
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:379) ~[na:1.7.0_67]
at org.apache.thrift.transport.TNonblockingSocket.read(TNonblockingSocket.java:142) ~[libthrift-0.9.2.jar:0.9.2]
at org.apache.thrift.server.AbstractNonblockingServer$FrameBuffer.internalRead(AbstractNonblockingServer.java:539) ~[libthrift-0.9.2.jar:0.9.2]
at org.apache.thrift.server.AbstractNonblockingServer$FrameBuffer.read(AbstractNonblockingServer.java:388) ~[libthrift-0.9.2.jar:0.9.2]
at org.apache.thrift.server.AbstractNonblockingServer$AbstractSelectThread.handleRead(AbstractNonblockingServer.java:203) ~[libthrift-0.9.2.jar:0.9.2]
at org.apache.thrift.server.TThreadedSelectorServer$SelectorThread.select(TThreadedSelectorServer.java:590) ~[libthrift-0.9.2.jar:0.9.2]
at org.apache.thrift.server.TThreadedSelectorServer$SelectorThread.run(TThreadedSelectorServer.java:545) ~[libthrift-0.9.2.jar:0.9.2]
很可能是直接内存没有做垃圾回收;
“垃圾收集进行时,虚拟机虽然会对Direct Memory进行回收,但是却不能像新生代和老年代一样,发现空间不足就通知收集器进行回收,它只能等到老年代满后做full gc,然后顺便帮它清理掉内存的废气对象”
如上引用自《深入理解java虚拟机》,程序可能一直在运行没有做过full gc,然后导致直接内存用光,另外直接内存默认值和堆大小一致