.Net 内存池

本文介绍在开发Pinpoint.Net客户端过程中遇到的内存管理问题及解决方案。通过使用Microsoft.IO.RecyclableMemoryStream项目,有效地减少了应用程序池的垃圾回收频率,并详细解释了RecyclableMemoryStreamManager的工作原理和使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  最近在开发 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方法,不然无法将内存释放回内存池,导致内存泄露!

转载于:https://www.cnblogs.com/YaungOu/p/7247324.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值