4

When I create a script bundle using Microsoft Web Optimization (bundling and minification for ASP.NET and MVC), does the server keep a copy of the bundle in memory? Or does it read from disk each time it gets a request to create the bundle? Read a number of blogs and articles on the subject but they only talk about the usage, benefits, etc.

I've even poked around the w3wp.exe process with WinDbg but I'm not smart enough or patient enough to find the bundles in memory to verify this. And just watching task manager doesn't seem reliable because obviously strings at some point will be loaded into memory, but .NET heap doesn't always shrink back down immediately. Thanks!

nothingisnecessary
  • 6,099
  • 36
  • 60
  • You could try https://technet.microsoft.com/en-us/sysinternals/bb896645 and see when/if the css files get hit. – Paul Abbott Jan 06 '17 at 23:03
  • Thank you, I forgot about procmon. It appears answer is Yes although I saw behavior I didn't quite understand when watching procmon for one specific .js file. With bundling enabled it wasn't clear to me how .NET was aware of a change made to the file (which updates the version hash) because I didn't notice a full read of the file after it was modified, though the script did behave differently on client side. Next step I guess I'm diving into code with ILSpy just cuz I'm curious, but I did get the original answer I was looking for, thanks – nothingisnecessary Jan 09 '17 at 19:18

1 Answers1

3

Short answer

Memory. But also remember that the browser already caches the information in the client.

Long answer

First of all, the bundle will be cached by the browser as it's said in the Bundling and Minification page:

Once you update one file in a bundle, a new token is generated for the bundle query string parameter and the full bundle must be downloaded the next time a client requests a page containing the bundle. In traditional markup where each asset is listed individually, only the changed file would be downloaded. Assets that change frequently may not be good candidates for bundling.

Bundling and minification primarily improve the first page request load time. Once a webpage has been requested, the browser caches the assets (JavaScript, CSS and images) so bundling and minification won’t provide any performance boost when requesting the same page, or pages on the same site requesting the same assets. If you don’t set the expires header correctly on your assets, and you don’t use bundling and minification, the browsers freshness heuristics will mark the assets stale after a few days and the browser will require a validation request for each asset

And also shown here, in the image taken from the same page, where they tested with Fiddler: Fiddler

So far we are safe as it's cached by the browser.

However, I went a bit further and created a small test project with this code in the Controller:

public ActionResult Index()
{
    return View(HttpRuntime.Cache);
}

And this code in the View:

<p>
    @Html.DisplayForModel()
</p>

Which gave me the following results:

  1. First run:

    :ViewCacheEntry:System.Web.Mvc.RazorViewEngine, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:View:Index:Home:::__AppStartPage__~/_appstart.cshtml
    :ViewCacheEntry:System.Web.Mvc.RazorViewEngine, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:View:Index:Home::Mobile:__AppStartPage__~/_appstart.vbhtml
    
  2. Second run:

    :ViewCacheEntry:System.Web.Mvc.RazorViewEngine, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:Partial:_LoginPartial:Home::Mobile:
    :ViewCacheEntry:System.Web.Mvc.RazorViewEngine, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:View:Index:Home:::System.Web.Optimization.Bundle:~/bundles/modernizr
    :ViewCacheEntry:System.Web.Mvc.RazorViewEngine, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:Partial:_LoginPartial:Home:::System.Web.Optimization.Bundle:~/bundles/bootstrap__AppStartPage__~/_appstart.cshtml
    :ViewCacheEntry:System.Web.Mvc.RazorViewEngine, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35:View:Index:Home::Mobile:System.Web.Optimization.Bundle:~/bundles/jquerySystem.Web.Optimization.Bundle:~/Content/css__AppStartPage__~/_appstart.vbhtml
    

On the second run you will see that modernizr, bootstrap, jquery and css (my bundles!) are in the cache. That would explain why if we load the same page in 2 different browsers we will get the same query string, even after being loaded 5mins apart:

  • Edge: Edge
  • Firefox Dev Edition: Firefox Dev Edition
Community
  • 1
  • 1
Davidson Sousa
  • 1,353
  • 1
  • 14
  • 34
  • 1
    Thanks for the thorough test, with procmon was able to confirm that server caches the bundled scripts memory. I was trying to measure impact of duplicating scripts (same script in different bundles) for a large application, and looks like those scripts do hang out in app pool memory until it is recycled and bundles are recreated – nothingisnecessary Jan 09 '17 at 19:20