C# : Out of Memory exception

string out of memory exception c#
system.outofmemoryexception fix
c# system outofmemoryexception solution
exception of type 'system.outofmemoryexception' was thrown. c# datatable
system.outofmemoryexception iis
out of memory exception sql
memory exception meaning
c# list out of memory

Today my application today threw an OutOfMemoryException. To me this was always almost impossible since I have 4GB RAM and a lot of virtual memory too. The error happened when I tried to add an existing collection to a new list.

List<Vehicle> vList = new List<Vehicle>(selectedVehicles);  

To my understanding there isn't much memory allocated here since the vehicles my new list should contain already exist inside the memory. I have to admit Vehicle is a very complex class and I tried to add about 50.000 items to the new list at once. But since all Vehicles in the application come from a database that is only 200MB in size: I have no idea what may cause an OutOfMemoryException at this point.

Two points:

  1. If you are running a 32 bit Windows, you won't have all the 4GB accessible, only 2GB.
  2. 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 your List, even though in total you have plenty of free memory.

C# : Out of Memory exception, If you're interested in more details about this subject, I recommend this article from Eric Lippert, a former Microsoft employee working on the C#  Common causes of C# System.OutOfMemoryException. Initializing an array which is not large enough, ensure the array size is correct! This is by far the most common cause of this exception. Reading very large data sets into memory, as demonstrated in the example above. Having too many active sessions or storing too much data in session state.

3 years old topic, but I found another working solution. If you're sure you have enough free memory, running 64 bit OS and still getting exceptions, go to Project properties -> Build tab and be sure to set x64 as a Platform target.

Debugging System.OutOfMemoryException using .NET tools, NET Exception Handling series, today we'll be looking over the amazing System.​OutOfMemoryException . As the name implies, the System. C# program that raises out-of-memory exception class Program { static void Main() {// Attempt to create a string of 2.1 billion chars. // This results in an out-of-memory error. This results in an out-of-memory error.

.Net4.5 does not have a 2GB limitation for objects any more. Add this lines to App.config

    <gcAllowVeryLargeObjects enabled="true" />    

and it will be possible to create very large objects without getting OutOfMemoryException

Please note it will work only on x64 OS's!

.NET Exception Handling - System.OutOfMemoryException, Understand the OutOfMemoryException, which occurs when not enough memory C# program that raises out-of-memory exception class Program { static void  All .NET exceptions are derived classes of the System.Exception base class, or derived from another inherited class therein. System.SystemException is inherited from the System.Exception class. System.OutOfMemoryException is inherited from the System.SystemException class.

Data stored in database compared to memory in your application is very different.

There isn't a way to get the exact size of your object but you could do this:


After a certain amount of objects have been loaded and see how much your memory is changing as you load the list.

If it is the list that is causing the excessive memory usage then we can look at ways to minimize it. Such as why do you want 50,000 objects loaded into memory all at once in the first place. Wouldn't it be best to call the DB as you require them?

If you take a look here: http://www.dotnetperls.com/array-memory you will also see that objects in .NET are greater than their actual data. A generic list is even more of a memory hog than an array. If you have a generic list inside your object then it will grow even faster.

C# OutOfMemoryException, I have created a C# console application which I need to run on 24/7 basis to continuously perform database requests and do heavy calculations  Out of that, 2GB is reserved for the operating system (Kernel-mode memory) and 2GB is allocated to user-mode processes. The 2GB allocated for Kernel-mode memory is shared among all processes, but each process gets its own 2GB of user-mode address space.

OutOfMemoryException (on 32-bit machines) is just as often about Fragmentation as actual hard limits on memory - you'll find lots about this, but here's my first google hit briefly discussing it: http://blogs.msdn.com/b/joshwil/archive/2005/08/10/450202.aspx. (@Anthony Pegram is referring to the same problem in his comment above).

That said, there is one other possibility that comes to mind for your code above: As you're using the "IEnumerable" constructor to the List, you may not giving the object any hints as to the size of the collection you're passing to the List constructor. If the object you are passing is is not a collection (does not implement the ICollection interface), then behind-the-scenes the List implementation is going to need to grow several (or many) times, each time leaving behind a too-small array that needs to be garbage collected. The garbage collector probably won't get to those discarded arrays fast enough, and you'll get your error.

The simplest fix for this would be to use the List(int capacity) constructor to tell the framework what backing array size to allocate (even if you're estimating and just guessing "50000" for example), and then use the AddRange(IEnumerable collection) method to actually populate your list.

So, simplest "Fix" if I'm right: replace

List<Vehicle> vList = new List<Vehicle>(selectedVehicles);


List<Vehicle> vList = new List<Vehicle>(50000);  

All the other comments and answers still apply in terms of overall design decisions - but this might be a quick fix.

Note (as @Alex commented below), this is only an issue if selectedVehicles is not an ICollection.

[Solved] System out of memory exception, OutOfMemoryException when initializing a list of objects. To me this seemed pretty much impossible because my machine has 4GB RAM. Which means that the large object heap gets fragmented and in my experience this inevitably leads to out of memory errors. In the original question the list has 50,000 items in it. Internally a list uses an array, and assuming 32 bit that requires 50,000 x 4bytes = 200,000 bytes (or double that if 64 bit).

C#, The System.OutOfMemoryException occurs when the CLR fail in allocating enough memory that is needed. System.OutOfMemoryException is  Since most programs request working memory from the OS and do not request a file mapping, they will be limited by the system's RAM and page file size. As noted in the comment by Néstor Sánchez (Néstor Sánchez) on the blog, with managed code like C# you are stuck to the RAM/page file limitation and the address space of the operating system.

How to capture out of memory exception in C#?, OutOfMemoryException' exceptions being thrown in your ASP.Net MVC application using C# and Entity Framework. Problem. Table of Contents. As the name suggests, the exception is thrown when a .NET application runs out of memory. There are a lot of blog posts out there, trying to explain why this exception occurs, but most of them are simply rewrites of the documentation on MSDN: System.OutOfMemoryException Class.

Exception of type 'System.OutOfMemoryException' was thrown , Hi guys, I got Out of Memory Exception while run the below code. I dont know how to resolve this. Will appreciate if somebody help me. foreach  We would like to show you a description here but the site won’t allow us.

  • What is the value (and type) of selectedVehicles?
  • When the OutOfMemoryException was thrown, did you attach to the process with a debugger and see what the problem might be? How big were the objects? The .NET Framework has a hard limit of 2 GB for object size, minus the overhead consumed by the framework itself.
  • Is Vehicle possibly a struct instead of a class?
  • 200MB of database space may easily take up more than twice that much when converting to .net objects. Afterward, it may be a smaller footprint, but the framework is trying to grab a large, contiguous chunk of memory at once that is not available.
  • Your statement about how much memory you have the amount of virutal memory you have allowed your system to have shows a lack of understanding how virtual memory and phyiscal memory work. You might want to read some information on that subject.
  • You only have 2 GB available in 64-bit Windows, too. This is a limitation of the .NET Framework, not just of the 32-bit address space.
  • @CodyGray That would be 2GB per object (array), not 2 GB total.
  • @CodyGray, I think the 2GB array limit is not in the new .NET version.
  • One can overcome it using gcAllowVeryLargeObjects configuration setting. More details here - msdn.microsoft.com/en-us/library/…
  • I can't see the image, what's the option?
  • Project properties -> Build tab -> change Platform target to x64
  • Nicely done. This worked for me, noting had to change the Build target to x64.
  • applies too to ASP.NET 4.5 ? Using local Report, My App v1 (asp.net 3.5 - clr 2.0 - classic ) works OK, but my App v2 (asp.net 4.5, clr 4.0, classic) generate OutOfMemoryException error, in the same IIS server
  • If selecyedVehicles is a collection, the constructor will allocate the correct array size. No need to go through AddRange.