首页 > 代码库 > .Net 内存池

.Net 内存池

  最近在开发 Pinpoint .Net 客户端,和服务端通信都是通过 TCP 或者 UDP,需要处理大量的 Byte 数据,使用 .Net Framework 只能通过 new Byte[] 的方式申请内存。客户端每秒钟处理的数据包非常多,通过这样方式容易导致应用程序池频繁进行 GC。刚好在Microsoft官方的github看到开源的 .Net 内存池项目,特意分享一下。传送门:https://github.com/Microsoft/Microsoft.IO.RecyclableMemoryStream

  内存池是在真正使用内存之前,先申请分配一定数量的、大小相等(一般情况下)的内存块留作备用。当有新的内存需求时,就从内存池中分出一部分内存块,若内存块不够再继续申请新的内存。RecyclableMemoryStreamManager 内部定义了两种类型的内存池,每种类型内存池的块大小可以根据需要设置,可以设置内存池的上限。我们不需要关注内存的分配,像平常使用 MemoryStream 一样调用 RecyclableMemoryStream 即可。

技术分享

(引用:http://www.philosophicalgeek.com/2015/02/06/announcing-microsoft-io-recycablememorystream/)

  

  说明:(摘自项目README)

  • The semantics are close to the original System.IO.MemoryStream implementation, and is intended to be a drop-in replacement.
  • Rather than pooling the streams themselves, the underlying buffers are pooled. This allows you to use the simple Dispose pattern to release the buffers back to the pool, as well as detect invalid usage patterns (such as reusing a stream after it’s been disposed).
  • The MemoryManager is thread-safe (streams themselves are inherently NOT thread safe).
  • Each stream can be tagged with an identifying string that is used in logging - helpful when finding bugs and memory leaks relating to incorrect pool use.
  • Debug features like recording the call stack of the stream allocation to track down pool leaks
  • Maximum free pool size to handle spikes in usage without using too much memory.
  • Flexible and adjustable limits to the pooling algorithm.
  • Metrics tracking and events so that you can see the impact on the system.

  用法:

var sourceBuffer = new byte[]{0,1,2,3,4,5,6,7}; 
var manager = new RecyclableMemoryStreamManager(); 
using (var stream = manager.GetStream()) 
{ 
    stream.Write(sourceBuffer, 0, sourceBuffer.Length); 
}

  

  注意:RecyclableMemoryStream 使用完,必须调用Dispose方法,不然无法将内存释放回内存池,导致内存泄露!

.Net 内存池