Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Bundling: Memory Leak?

We've recently implemented ASP.NET bundling and minification in our app.

This was supposed to make it faster... and it speeds up the download, but since it went live memory usage has gone up.

I just did a memory dump as per ASP.Net Worker Process Memory Profile Tools and looked at the results from WinDbg, and things that are eating far the most memory

00007ffd809d0ee0     4478      1676592 System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[System.Xml.XmlDictionaryString, System.Runtime.Serialization]][]
00007ffdcd9bed78    14077      1689240 System.Reflection.RuntimeParameterInfo
00007ffdcd9bbb58    44987      2159376 System.Text.StringBuilder
00007ffd81166738    80376      2572032 Microsoft.Ajax.Utilities.CssContext
00007ffdcd9b9220    41647      2867112 System.Int32[]
00007ffdcd9bf100    27654      3097248 System.Reflection.RuntimeMethodInfo
00007ffd81166600    80364      3214560 Microsoft.Ajax.Utilities.CssToken
00007ffdcd9bd1e0    11875      4132224 System.Collections.Hashtable+bucket[]
00007ffdcd9b6fc0    37793      4331968 System.Object[]
00007ffdcd9b7a98    50153      8237258 System.Char[]
0000003495080610    41499      9288392      Free
00007ffd81166868   401856      9644544 Microsoft.Ajax.Utilities.Position
00007ffdcd9b6948   520475     31605586 System.String
00007ffdcd9bc988    38797     86558836 System.Byte[]

(at the bottom of the list) are all Microsoft.Ajax.Utilities classes.

So it looks like bundling is somehow leaking memory. Are there any common reasons why this could be happening? So much of the bundling stuff happens inside black boxes it's hard to know what we're doing wrong. We call

BundleConfig.RegisterBundles(BundleTable.Bundles);

in Application_Start and that's about it, I think.

like image 688
Ben Curthoys Avatar asked Oct 20 '25 10:10

Ben Curthoys


1 Answers

We had the same issue with our ScriptBundles back a while ago, and then recently started having it with our StyleBundles.

If BundleTable.EnableOptimizations is true and you are using ScriptBundle or StyleBundle, it attempts to minify the JS and CSS files itself, rather than using the already-minified files that exist.

At least in the StyleBundle, it builds all the minified CSS into a StringBuilder and then calls .ToString() on that. However, if the resulting string containing all your minified CSS is too large, it throws an OutOfMemoryException.

The solution I've found to this is to change ScriptBundle/StyleBundle to just Bundle. This makes it used the already-minified JS/CSS files that exist, rather than trying to do all that work itself.

So for example, instead of this:

// kendo bundle
bundles.Add(new ScriptBundle("~/bundles/kendo")
    .Include("~/Scripts/kendo/2021.3.1109/kendo.all.min.js",
             "~/Scripts/kendo/2021.3.1109/kendo.aspnetmvc.min.js",
             "~/Scripts/kendo.modernizr.custom.js"
));

Do this:

// kendo bundle
bundles.Add(new Bundle("~/bundles/kendo")
    .Include("~/Scripts/kendo/2021.3.1109/kendo.all.min.js",
             "~/Scripts/kendo/2021.3.1109/kendo.aspnetmvc.min.js",
             "~/Scripts/kendo.modernizr.custom.js"
));
like image 189
CarenRose Avatar answered Oct 22 '25 00:10

CarenRose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!