#SharePointProblems | Koskila.net

Solutions are worthless unless shared! Antti K. Koskela's Personal Professional Blog

Exception of type 'System.OutOfMemoryException' was thrown.

koskila
Reading Time 3 min
Word Count 431 words
Comments 2 comments
Rating 4 (1 votes)
View

This post describes one of the more no-brainerish ways of fixing a 'System.OutOfMemoryException' exception being thrown in your ASP.Net MVC application using C# and Entity Framework.

Problem

While developing a web project, for example, an ASP.NET MVC web application that is using EF, sometimes when handling a lot of data or complex entities on your dev machine, you encounter this error:

OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
   System.Text.StringBuilder.ToString() +35
   System.IO.StreamReader.ReadToEnd() +123
   System.Web.Optimization.BundleFile.ApplyTransforms() +74
   System.Web.Optimization.DefaultBundleBuilder.BuildBundleContent(Bundle bundle, BundleContext context, IEnumerable`1 files) +472
   System.Web.Optimization.Bundle.GenerateBundleResponse(BundleContext context) +127
   System.Web.Optimization.Bundle.GetBundleResponse(BundleContext context) +45
   System.Web.Optimization.BundleResolver.GetBundleContents(String virtualPath) +166
   System.Web.Optimization.AssetManager.DeterminePathsToRender(IEnumerable`1 assets) +205
   System.Web.Optimization.AssetManager.RenderExplicit(String tagFormat, String[] paths) +35
   System.Web.Optimization.Scripts.RenderFormat(String tagFormat, String[] paths) +107
   System.Web.Optimization.Scripts.Render(String[] paths) +21

This of course blocks you from debugging or even running at least this part of your code locally. What to do?

Reason

By default, Visual Studio uses a 32-bit version of IIS Express for your deployments. A lot of times, this is good and intended, and not a problem. In some rare cases, this might mean, however, that your IIS processes are running out of memory.

I'm going to argue, that most of the time you shouldn't end up having this issue if your code is sane and smart. Most of the time, if you get this error, you're building infinite loops or forcing the Entity Framework to load millions and millions of rows in memory. Fix that first.

However, with EF it's somewhat easy to build apps that use a lot of memory, but work as they should. Perhaps your architecture does make sense, and using a lot of memory is ok?

In this case, you might want to accommodate this memory requirement by enabling a 64-bit version of IIS locally. That's probably what you're using in production anyway.

This is luckily a pretty easy change.

How to solve System.OutOfMemoryException in Visual Studio?

The first thing is to make sure you're not hogging all the available memory for no reason - no neverending loops or infinite recursion! After that, consider the following.

Time needed: 5 minutes.

How to enable 64-bit IIS Express from Visual Studio settings

  1. Navigate to the following settings page:

    Visual Studio > Tools > Options > Projects and Solutions > Web Projects

  2. Enable the following setting

    "Use the 64-bit version of IIS Express for websites and projects" (see screenshot 1 below)
    How to enable the 64-bit version of IIS Express in Visual Studio 2017
    Screenshot 1: How to enable the 64-bit version of IIS Express in Visual Studio 2017

  3. Also, don't forget to verify you're building a 64-bit version of your app!

    See screenshot 2 below:
    64-bit build option for the solution in Visual Studio
    Screenshot 2: 64-bit build option for the solution in Visual Studio

And you should be good to go!

Comments

Interactive comments not implemented yet. Showing legacy comments migrated from WordPress.
Vihaan
2019-12-08 20:31:59)
Hi! Very nice post, this is certainly a pretty goofy cause for this error! Here is a few other tips I'd like to share for anyone else in a situation where the error is not caused by the 64-bit IIS Express flag. 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. - Applying a complex regex (regular expression) to a large file or string. - Downloading, streaming or uploading large files. - You are calling the StringBuilder.Insert method and are repeatedly concatenating strings. - You are not implementing the Implementing a Dispose method when dealing with unmanaged resources, and your app is leaking. - You are storing very large sets of data in memory. An example could be querying large database data sets via LINQ in memory. Snippet borrowed from: system.outofmemoryexception. Hopefully this helps someone. Happy Holidays!
2019-12-08 20:47:00
Hi Vihaan, Thanks for your comment! I agree - mine was probably one of the goofier ways to get this error :) Appreciate the tips and tricks! Happy holidays to you too!
Whitewater Magpie Ltd.
© 2025
Static Site Generation timestamp: 2025-08-26T05:15:54Z