Click here to Skip to main content
15,889,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Deal All

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

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

My Code :-

C#
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 :-
C#
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);



OutOfMemoryException.


Any Solution please help.


Thanks.

What I have tried:

CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
Posted
Updated 6-Sep-16 1:14am
Comments
InbarBarkai 6-Sep-16 6:21am    
How long is your text?
Member 12352638 6-Sep-16 9:32am    
750MB
Mehdi Gholam 6-Sep-16 6:32am    
Make sure your process is 64bit not 32bit.
Member 12352638 6-Sep-16 9:32am    
Process is 64bit
Philippe Mori 6-Sep-16 9:04am    
If your data is large, then you should process it by blocks. You should avoid working with data that is larger than a few hundreds megabytes if alternative algorithms are possible.

1 solution

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. During re-allocation more memory is required (old size * 3)
  2. The newly allocated block must be contiguous
  3. With 32-bit applications the limit of 2 GB may be reached

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.

[EDIT]
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]
 
Share this answer
 
v2
Comments
Member 12352638 6-Sep-16 8:26am    
Thank U for replay.

Actually it depends on RAM or what?

My System RAM :- 4GB
OS - 64byte
Jochen Arndt 6-Sep-16 8:40am    
It does not depend on RAM.
The system uses the disk when there is no more free RAM (the process is called swapping).

But 32-bit applications have a limit of 2 GB per process (3 GB with an optional setting). If you have a 32-bit application you are either exceeding this limit (your process has also other memory allocated) or you have no free blocks of contigous memory.

It is all explained in the article link from my answer.

You should also try my suggestion to set the capacity in advance. It may solve your problem. If not, you must think about a redesign (e.g. using the method from CP article).
Mehdi Gholam 6-Sep-16 10:04am    
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900