异常内存流中的Outofmemory [英] Outofmemory in exception memorystream

查看:161
本文介绍了异常内存流中的Outofmemory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部交易



通过MemoryStream写入字节数组数据时出现错误OutOfMemory异常。



我的PC-64bit

RAM- 4GB

DataSize-750MB- 786432000



我的代码: -



Deal All

I got error OutOfMemory Exception when Write Byte Array data through MemoryStream.

My PC-64bit
RAM- 4GB
DataSize-750MB- 786432000

My Code :-

using (MemoryStream MemStream = new MemoryStream())
                    {
                        using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
                        {
                            CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);

                            //System.Buffer.BlockCopy(PlainTextBytes, 0, MemStream.ToArray(), 0, PlainTextBytes.Length);

                            CryptoStream.FlushFinalBlock();
                            CipherTextBytes = MemStream.ToArray();
                            MemStream.Close();
                            CryptoStream.Close();
                        }
                    }





接近错误: -



Error coming Near :-

CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);







OutOfMemoryException。





任何解决方案请帮忙。





谢谢。



我的尝试:



CryptoStream.Write(PlainTextBytes,0,PlainTextBytes.Length);




OutOfMemoryException.


Any Solution please help.


Thanks.

What I have tried:

CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);

推荐答案

CodeProject上有一篇文章这解释了发生了什么以及可以做些什么: MemoryStream的替代品 [ ^ ]。



用我自己的话说一个简短的解释:



MemoryStream 这样的对象最初不知道所需内存的大小。因此,他们将从相当少量的已分配内存开始。当这个内存用完时,它们将分配一个通常大小为两倍的新块,复制前一个块的内容并释放它。当需要大量内存时,这可能会导致内存异常,原因有三:

There is an article here at CodeProject that explains what is happening and what can be done: A replacement for MemoryStream[^].

A short explanation with my own words:

Objects like MemoryStream initially don't know about the size of the required memory. So they will start with a rather small amount of allocated memory. When this memory is used up, they will allocate a new block of typically twice the size, copy the content of the previous block and free that. When needing large amounts of memory this may result in out memory exceptions for three reasons:


  1. 在重新分配期间需要更多内存(旧版本) * 3)
  2. 新分配的块必须是连续的
  3. 对于32位应用程序,可以达到2 GB的限制



用750 MB的例子解释一下:

假设流实际上保持700 MB,它将尝试分配1400 MB。当没有这种大小的自由连续块时,这将失败。考虑到现有的700 MB,您的流实际上拥有的内存超过了已安装内存的一半。





如果您事先知道所需的大小,则应使用接受 capacity 参数的构造函数。这避免了多次重新分配和分配未使用的内存,如果分配失败,您将立即获得异常。

[/ EDIT]


To explain this with your example of 750 MB:
Assuming that the stream actually holds 700 MB, it will try to allocate 1400 MB. This will fail when there is no free contiguous block of this size. Having the existing 700 MB in mind, your stream would actually own more than the half of your installed memory at that time.


If you know the required size in advance, you should use the constructor that accepts the capacity argument. This avoids multiple re-allocations and allocating unused memory, and you will get an immediate exception if allocation fails.
[/EDIT]


这篇关于异常内存流中的Outofmemory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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