确定共享主机中asp.net缓存使用的内存

时间:2012-05-31 09:14:38

标签: c# asp.net caching shared-hosting

根据这个question,我想知道asp.net的system.web.caching.cache是​​否适合我,或者我应该使用数据库缓存?

所以,我需要知道system.Web.caching.cache正在使用多少内存? 但由于我使用的是共享托管服务器,因此我无法使用任务管理器。 有没有办法确定system.web.caching.cache使用一些代码使用了多少内存?

1 个答案:

答案 0 :(得分:6)

查看应用程序使用多少工作内存的一种快速方法是直接询问垃圾收集。

long bytes = GC.GetTotalMemory(false);
txtMemoryUsed.Text = bytes.ToString();

并使用此文字<asp:Literal runat="server" ID="txtMemorysUsed" EnableViewState="false" />

但您可以使用PerformanceCounter获取更多详细信息,例如,您可以获取此代码使用的池的虚拟内存量:

 var oPerfCounter = new PerformanceCounter();
oPerfCounter.CategoryName = "Process";
oPerfCounter.CounterName = "Virtual Bytes";
oPerfCounter.InstanceName = "aspnet_wp";
txtMemorysUsed.Text = "Virtual Bytes: " + oPerfCounter.RawValue + " bytes";

您可以使用所有这些参数来获取池的信息。

Processor(_Total)\% Processor Time
Process(aspnet_wp)\% Processor Time
Process(aspnet_wp)\Private Bytes
Process(aspnet_wp)\Virtual Bytes
Process(aspnet_wp)\Handle Count
Microsoft® .NET CLR Exceptions\# Exceps thrown / sec
ASP.NET\Application Restarts
ASP.NET\Requests Rejected
ASP.NET\Worker Process Restarts (not applicable to IIS 6.0)
Memory\Available Mbytes
Web Service\Current Connections
Web Service\ISAPI Extension Requests/sec

例如,此参数获取cpu负载。

oPerfCounter.CategoryName = "Processor";
oPerfCounter.CounterName = "% Processor Time";
oPerfCounter.InstanceName = "_Total";
txtOutPut.Text = "Current CPU Usage: " + oPerfCounter.NextValue() + "%";

参考:http://msdn.microsoft.com/en-us/library/ms972959.aspx

relative:Monitoring ASP.NET application memory from within the application

我在本地iis上测试过并且有效。