none
Calling HttpApplication.UnloadAppDomain causes slowdown in C++/CLI library code RRS feed

  • Question

  • Here is a Visual Studio 2017 solution containing two projects:

    https://www.dropbox.com/s/yx25jmjreavo5gf/DomainApp.zip?dl=0&m=

    DomainApp: ASP.NET MVC web app. Index.cshtml has been modified to make JavaScript calls to the HomeController.Run action with either “reset” or “run” action as follows

     

         public ActionResult Run(string command)

          {

             if(command == "reset")

             {

                // Unload the domain

                HttpRuntime.UnloadAppDomain();

                return Content("Reset at " + DateTime.Now.ToLongTimeString());

             }

     

             // Make the unmanaged call and return elapsed time in milliseconds

             var mark = DateTime.Now;

             thelib.Class1.Run();

             var elapsed = (int)(DateTime.Now - mark).TotalMilliseconds;

             return Content($"Run took {elapsed} ms at {DateTime.Now.ToLongTimeString()}");

          }

     

    thelib is a C++/CLI library (IJW) created from the Visual Studio template (Project/New/C++/CLR/Class Library). It contains

    1.       Managed class calling an unmanaged function

    2.       The unmanaged function calls another managed function in a loop (to exaggerate the slowness)

    3.       An native class with a global static instance.

     

    Here is the full code:

     

    // Unmanaged code

    #pragma managed(push, off)

    // An unmanaged class

    class MyNativeClass

    {

       public: MyNativeClass() { }

    };

     

    static MyNativeClass globalInstance;

     

    #pragma managed(pop)

    // A managed function

    static int managedFuntion(int x)

    {

       return x * 2;

    }

     

    #pragma managed(push, off)

    // An umanaged function that calls the managed function

    static void unmanagedFunction()

    {

       int sum = 0;

       for(int i = 0; i < 100000; i++)

       {

          // Call the managed function

          sum += managedFuntion(i);

       }

    }

     

    #pragma managed(pop)

     

    void thelib::Class1::Run()

    {

       unmanagedFunction();

    }

     

    Run the app, and click on the ‘run’ button a few times. Note that the result is very fast, about 1 ms. Click the ‘reset’ button once to call ‘HttpApplication.UnloadAppDomain’ and then click ‘run’ again. Now the action takes 500ms. The only way to get rid of this slowness is to kill the app and restart it.

     

    Notes:

    -          The solution uses x86 by default and runs in IIS express. Same problem happens with x64 or in full IIS.

    -          If I remove the global static instance, then the problem goes away

    -          If I make the native class singleton (move the globalInstance variable inside the class) then the problem goes away

    -          All of the above is not valid in my case because I am using a massive amount of unmanaged source code and modifying all is not an option.

     

    Any hints on what is causing the slowness after HttpApplication.UnloadAppDomain and how to fix it?


    Tuesday, October 3, 2017 5:47 PM

All replies

  • Hi lke Starnes,

    According to your question is more related to ASP.NET, you could post a new thread in ASP.NET forum for suitable support.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions. 

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 4, 2017 5:50 AM