.NET Out Of Memory Exception - Used 1.3GB but have 16GB installed
There is no difference until you compile to same target architecture. I suppose you are compiling for 32
bit architecture in both cases.
It's worth mentioning that OutOfMemoryException
can also be raised if you get 2GB
of memory allocated by a single collection in CLR (say List<T>
) on both architectures 32
and 64
bit.
To be able to benefit from memory goodness on 64
bit architecture, you have to compile your code targeting 64
bit architecture. After that, naturally, your binary will run only on 64
bit, but will benefit from possibility having more space available in RAM.
C# : Out of Memory exception
Two points:
- If you are running a 32 bit Windows, you won't have all the 4GB accessible, only 2GB.
- Don't forget that the underlying implementation of
List
is an array. If your memory is heavily fragmented, there may not be enough contiguous space to allocate yourList
, even though in total you have plenty of free memory.
Out of memory exception even for a very small task
I tried and I can reproduce your problem. It is definitely out of memory and nothing like "It just seems to be" the memory usage increases to about 4 GB and then the error shows up. Console output is just to see what's happening there.
The Image object seems to be not the best way to save the data.
I tried this and got this to work with many many files. Maybe you can change the code to fit your needs:
String[] allfiles = Directory.GetFiles(imagePath, "*.*", SearchOption.AllDirectories);
//List<Image> allImages = new List<Image>();
List<Byte[]> allImagesBytes = new List<Byte[]>();
foreach (string file in allfiles)
{
using (FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
if (IsImage(stream))
{
Console.Clear();
Console.Write(allImagesBytes.Count());
//allImages.Add(Image.FromStream(stream));
//allImages.Add(Image.FromFile(file));
allImagesBytes.Add(File.ReadAllBytes(file));
}
}
}
.NET OutOfMemory exception with PAE
What counts is not how much memory the system has available, rather what matters is how much memory your process has available. Since your process is a 32 bit process there is a hard limit of 4GB.
So you don't have 4GB free memory, the system does. You have used your allocation of 4GB and you are out of memory.
The only way forward is to move to a 64 bit process. Clearly that requires a 64 bit system.
Related Topics
How to Run an Exe Program from a Windows Service Using C#
C# Member Variable Initialization; Best Practice
How to Determine Programmatically Whether a Particular Process Is 32-Bit or 64-Bit
Deserializing JSON Object Array with JSON.Net
When Should I Dispose of a Data Context
How to Send Emails Through Ssl Smtp with the .Net Framework
At the End of an Async Method, Should I Return or Await
What Is the Purpose of Self Tracking Entities
How to Serialize/Deserialize to 'Dictionary<Int, String>' from Custom Xml Not Using Xelement
How to Set a Property Value with Reflection
Xaml Binding Not Working on Dependency Property
How to Ignore JSONproperty(Propertyname = "Somename") When Serializing JSON
Format Excel Column to Decimal Doing Export from C#
What Is [Serializable] and When Should I Use It