.NET Release code very slow when running a method for the first time. How to fix it withough NGen?

后端 未结 3 1325
清歌不尽
清歌不尽 2021-01-06 16:48

I have deployed a application in release mode (x64) which I expected to be fast, and I noticed that there is a severe slowdown whenever a new method, or set of methods is ex

3条回答
  •  無奈伤痛
    2021-01-06 17:20

    My first solution was to manually create dummy instances of classes and then call the methods with an optional dummy bool variable which would be false by default. This worked but required adding this dummy to every method which is crude and time consuming.

    Lucas Trzesniewski's answer is far more through as it prepares all classes and all methods in the code and it does not require any changes to any of my code so it is much easier to implement. The only cost is that by being so thorough it takes longer to complete at about .5 to 1.5 seconds or maybe .5 to 1 second longer than my selective start-up. More than worth it in my case to ensure no delays happen in code.

    There was no noticeable spike in memory or CPU usage other than the initial start-up, and memory usage actually slightly decreased, possibly because all code is now complied and optimized.

提交回复
热议问题