Should I use the static cached ResourceManager or a new instance for each web request? Does it matter?

落爺英雄遲暮 提交于 2019-12-07 03:08:24

问题


What, if any are the performance (or other) implications of creating a new .NET ResourceManager on every request with new ResourceManger(myResourceType.FullName, myResourceType.Assembly) vs using the "cached ResourceManager instance" in the .Designer.cs generated class (MyResourceType.ResourceManager)?

I am working in the context of a ASP.NET MVC 3 application using .resx files.

Edit: I am interested in implications beyond the cost allocating memory for the new object.

Edit: Looking at the MSDN documentation for ResourceManager.ReleaseAllResources, it states that:

This method will shrink the working set in a running application. Any future resource lookups on this ResourceManager will be as extensive as the first lookup, since it will need to search and load resources again.

This seems to imply that the initial opening of the resource set is expensive, which suggests to me that creating a new manager on each request could be expensive. However, the docs don't suggest a best practice with regard to the lifetime/scope of resource managers.


回答1:


I did some primitive profiling (using MiniProfiler) of the difference between using a cached manager (I used reflection to find the static cached manager for each resource type) and using a new manager for each key access. The results suggested that the new manager took about 45 times as long, which suggests to me that there is a real performance benefit to using the cached manager approach. However, both approaches were so fast that the difference probably doesn't matter much in practice.




回答2:


You will have an allocation of an object when creating it.

that means that you will have a performance and memory implications for creating the object over and over again.

you can try it and check how many garbage collection cycles you have (using performance counters) vs the amount of the "cached" resource manager instance which is created once (per process) and therefor there are no unnecessary allocations



来源:https://stackoverflow.com/questions/11160283/should-i-use-the-static-cached-resourcemanager-or-a-new-instance-for-each-web-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!