由dubbo服务禁用system.gc而引起的思考
2019-02-21 06:39:27来源:博客园 阅读 ()
1 final class SocketReceiveBufferPool { 2 3 private static final int POOL_SIZE = 8; 4 5 @SuppressWarnings("unchecked") 6 private final SoftReference<ByteBuffer>[] pool = new SoftReference[POOL_SIZE]; 7 8 SocketReceiveBufferPool() { 9 super(); 10 } 11 12 final ByteBuffer acquire(int size) { 13 final SoftReference<ByteBuffer>[] pool = this.pool; 14 for (int i = 0; i < POOL_SIZE; i ++) { 15 SoftReference<ByteBuffer> ref = pool[i]; 16 if (ref == null) { 17 continue; 18 } 19 20 ByteBuffer buf = ref.get(); 21 if (buf == null) { 22 pool[i] = null; 23 continue; 24 } 25 26 if (buf.capacity() < size) { 27 continue; 28 } 29 30 pool[i] = null; 31 32 buf.clear(); 33 return buf; 34 } 35 36 ByteBuffer buf = ByteBuffer.allocateDirect(normalizeCapacity(size)); 37 buf.clear(); 38 return buf; 39 } 40 41 final void release(ByteBuffer buffer) { 42 final SoftReference<ByteBuffer>[] pool = this.pool; 43 for (int i = 0; i < POOL_SIZE; i ++) { 44 SoftReference<ByteBuffer> ref = pool[i]; 45 if (ref == null || ref.get() == null) { 46 pool[i] = new SoftReference<ByteBuffer>(buffer); 47 return; 48 } 49 } 50 51 // pool is full - replace one 52 final int capacity = buffer.capacity(); 53 for (int i = 0; i< POOL_SIZE; i ++) { 54 SoftReference<ByteBuffer> ref = pool[i]; 55 ByteBuffer pooled = ref.get(); 56 if (pooled == null) { 57 pool[i] = null; 58 continue; 59 } 60 61 if (pooled.capacity() < capacity) { 62 pool[i] = new SoftReference<ByteBuffer>(buffer); 63 return; 64 } 65 } 66 } 67 68 private static final int normalizeCapacity(int capacity) { 69 // Normalize to multiple of 1024 70 int q = capacity >>> 10; 71 int r = capacity & 1023; 72 if (r != 0) { 73 q ++; 74 } 75 return q << 10; 76 } 77 }
1 private boolean read(SelectionKey k) { 2 final SocketChannel ch = (SocketChannel) k.channel(); 3 final NioSocketChannel channel = (NioSocketChannel) k.attachment(); 4 5 final ReceiveBufferSizePredictor predictor = 6 channel.getConfig().getReceiveBufferSizePredictor(); 7 final int predictedRecvBufSize = predictor.nextReceiveBufferSize(); 8 9 int ret = 0; 10 int readBytes = 0; 11 boolean failure = true; 12 13 ByteBuffer bb = recvBufferPool.acquire(predictedRecvBufSize); 14 15 try { 16 while ((ret = ch.read(bb)) > 0) { 17 readBytes += ret; 18 if (!bb.hasRemaining()) { 19 break; 20 } 21 } 22 failure = false; 23 } catch (ClosedChannelException e) { 24 // Can happen, and does not need a user attention. 25 } catch (Throwable t) { 26 fireExceptionCaught(channel, t); 27 } 28 29 if (readBytes > 0) { 30 bb.flip(); 31 32 final ChannelBufferFactory bufferFactory = 33 channel.getConfig().getBufferFactory(); 34 final ChannelBuffer buffer = bufferFactory.getBuffer(readBytes); 35 buffer.setBytes(0, bb); 36 buffer.writerIndex(readBytes); 37 //if(buffer instanceof BigEndianHeapChannelBuffer){ 38 // logger2.info("buffer instanceof BigEndianHeapChannelBuffer."); 39 //} 40 recvBufferPool.release(bb); 41 42 // Update the predi||\\||||| 43 predictor.previousReceiveBufferSize(readBytes); 44 45 // Fire the event. 46 fireMessageReceived(channel, buffer); 47 } else { 48 recvBufferPool.release(bb); 49 } 50 51 if (ret < 0 || failure) { 52 k.cancel(); // Some JDK implementations run into an infinite loop without this. 53 close(channel, succeededFuture(channel)); 54 return false; 55 } 56 57 return true; 58 }
原文链接:https://www.cnblogs.com/mantu/p/10409637.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 聊聊微服务架构及分布式事务解决方案! 2020-06-10
- java环境教程:Tomcat下载,安装,设置为Windows服务,启动 2020-06-09
- Dubbo+Zookeeper集群案例 2020-06-09
- Spring Cloud微服务(一):公共模块的搭建 2020-06-07
- 最强Dubbo面试题,附带超级详细答案 2020-06-06
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash