asp.netbundling-and-minificationasp.net-bundling

Does ASP.NET store bundled scripts in memory?


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!


Solution

  • 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: