IIS8

Exception of type ‘System.OutOfMemoryException’ was thrown.

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

Problem

While developing a web project, for example ASP.NET MVC with 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

Reason

By default, Visual Studio uses 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. With EF it’s somewhat easy to build apps that use a lot of memory, though. 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 64-bit version of IIS. 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:

  • You’ll need to enable 64-bit IIS Express from VS settings:
    • Visual Studio – Tools – Options – Projects and Solutions – Web Projects – “User the 64 bit version of IIS Express for web sites and projects”, screenshot below:
IIS Express 64-bit version
IIS Express 64-bit version
  • Also, don’t forget to verify you’re building 64-bit version of your app!
64-bit build option
64-bit build option

More:

  • Found this tip from Stack Overflow (disregard the memory hacks – shouldn’t be necessary).
The following two tabs change content below.

Antti K. Koskela

Solutions Architect / Escalations Engineer at Koskila / Norppandalotti Software / Valo Solutions
Antti Koskela is a proud digital native nomadic millenial full stack developer (is that enough funny buzzwords? That's definitely enough funny buzzwords!), who works as a Solutions Architect for Valo Intranet, the product that will make you fall in love with your intranet. Working with the global partner network, he's responsible for the success of Valo deployments happening all around the world. He's been a developer from 2004 (starting with PHP and Java), and he's been bending and twisting SharePoint into different shapes since MOSS. Nowadays he's not only working on SharePoint, but also on .NET projects, Azure, Office 365 and a lot of other stuff. This is his personal professional (e.g. professional, but definitely personal) blog.

Let me know your thoughts!