How to determine memory usage in my .NET application

早过忘川 提交于 2021-02-07 12:23:32

问题


I have a website where some user can upload his dll. I need to create an instance from this dll and call a specific method. Moreover, I need to do it with several dll's in 1 process, so it is not an option for me to use System.Diagnostics.Process.

Is there a way to determine how much memory does this method call use or limit it in my application runtime? Thanks in advance for any help


回答1:


Memory usage in .Net is very difficult to gauge - It will depend on a lot of factors like whether garbage collection run, how much of the framework has been loaded, etc.

You can look at memory usage on a per-process level using performance counters

Assuming for a moment you've already considered the security implications of running a users code in a semi-trusted environment, you need to use an AppDomain to load the DLL - That way it can be unloaded without restarting the whole website / worker pool




回答2:


If you use .NET 4.0 have a look at MemoryFailPoint.

Another suggestion: .NET 4.0 now offers facilities to supervise the memory consumption of an AppDomain (AppDomain.MonitoringIsEnabled Property). You can for instance use the main AppDomain to poll and tear down an AppDomain, if it uses too much memory.




回答3:


Could you use GC.GetTotalMemory as an alternative to track your memory usage?

http://msdn.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx




回答4:


Seriously you cannot go past Red Gates Ants Memory Profiler - free 14 day trial...



来源:https://stackoverflow.com/questions/4788358/how-to-determine-memory-usage-in-my-net-application

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