如何强制通过的MemoryStream占用释放内存? [英] How do I force release memory occupied by MemoryStream?

查看:529
本文介绍了如何强制通过的MemoryStream占用释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

const int bufferSize = 1024 * 1024;
var buffer = new byte[bufferSize];
for (int i = 0; i < 10; i++)
{
    const int writesCount = 400;
    using (var stream = new MemoryStream(writesCount * bufferSize))
    {
        for (int j = 0; j < writesCount; j++)
        {
            stream.Write(buffer, 0, buffer.Length);
        }
        stream.Close();
    }
}

我运行一个32位计算机上。

which I run on a 32-bit machine.

第一次迭代完成就好了,然后下一个迭代我得到就行了的System.OutOfMemoryException 异常 S中的的MemoryStream

The first iteration finishes just fine and then on the next iteration I get a System.OutOfMemoryException exception on the line that news the MemoryStream.

为什么不是previous 的MemoryStream 内存尽管回收使用语句?如何强制使用的内存释放的MemoryStream

Why isn't the previous MemoryStream memory reclaimed despite using statement? How do I force release of memory used by the MemoryStream?

推荐答案

我不认为这个问题是垃圾收集器不能做的工作。如果GC正在内存pressure它应该运行并收回你刚刚分配的400 MB的。

I don't think the problem is the garbage collector not doing its job. If the GC is under memory pressure it should run and reclaim the 400 MBs you've just allocated.

这是更可能下降到GC没有找到一个 contigious 400 MB 块。

This is more likely down to the GC not finding a contigious 400 MB block.

相反,一个内存不足的错误发生,因为这个过程是无法   找到的连续未使用的页面有足够大的部分其   虚拟地址空间执行请求的映射。

Rather, an "out of memory" error happens because the process is unable to find a large enough section of contiguous unused pages in its virtual address space to do the requested mapping.

您应该阅读埃里克利珀的博客文章<一个href="http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx"相对=nofollow>内存不足并不是指物理内存

You should read Eric Lippert's blog entry "Out Of Memory" Does Not Refer to Physical Memory

您是好得多做下面的两个

  1. 重用你已经分配的内存块(你为什么要创建一个使用完全相同的尺寸)
  2. 分配更小的块(小于 85KBs

此前DOTNET 4.5,DOTNET构建了两个堆,小对象堆(SOH)大对象堆(LOH)。请参阅<一href="http://blogs.msdn.com/b/dotnet/archive/2011/10/04/large-object-heap-improvements-in-net-4-5.aspx"相对=nofollow>大对象Hearp改进在.NET 4.5 通过布兰登布雷。你的的MemoryStream 被分配在LOH,而不是压缩(碎片整理)为过程的持续时间,使得它更可能是多次调用分配此大量的内存会抛出一个 OutOfMemoryException异常

Prior to Dotnet 4.5, Dotnet constructed two heaps, Small Object Heap (SOH) and Large Object Heap (LOH). See Large Object Hearp Improvements in .NET 4.5 by Brandon Bray. Your MemoryStream is being allocated in LOH, and not compacted (defragmented) for the duration of the process, making it much more likely that multiple calls to allocate this large amount of memory will throw an OutOfMemoryException

在CLR管理两个不同的堆分配,小对象   堆(SOH)和大对象堆(LOH)。任何分配更大   大于或等于85000个字节的推移蕙。复制大对象   有性能损失,因此LOH不板结​​不像SOH。   另一个定义特征是LOH只收集   期间,第2代集合。总之,这些有内置的   假设大对象分配是罕见的。

The CLR manages two different heaps for allocation, the small object heap (SOH) and the large object heap (LOH). Any allocation greater than or equal to 85,000 bytes goes on the LOH. Copying large objects has a performance penalty, so the LOH is not compacted unlike the SOH. Another defining characteristic is that the LOH is only collected during a generation 2 collection. Together, these have the built-in assumption that large object allocations are infrequent.

这篇关于如何强制通过的MemoryStream占用释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆