您现在的位置是:首页 > web开发 > .Net 内存池

.Net 内存池

web开发作者:dayu日期:5天前点击:2

(引用: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 内存池

原文地址:http://www.cnblogs.com/YaungOu/p/7247324.html


下一篇       上一篇