最近使用Thrift TThreadedSelectorServer服務方式,運行一段時間就會拋OutOfMemoryError: Direct buffer memory異常;java
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]
極可能是直接內存沒有作垃圾回收;apache
「垃圾收集進行時,虛擬機雖然會對Direct Memory進行回收,可是卻不能像新生代和老年代同樣,發現空間不足就通知收集器進行回收,它只能等到老年代滿後作full gc,而後順便幫它清理掉內存的廢氣對象」code
如上引用自《深刻理解java虛擬機》,程序可能一直在運行沒有作過full gc,而後致使直接內存用光,另外直接內存默認值和堆大小一致server