自定义计数器文件视图内存不足



我有一个带有一个 Web 角色的 Azure 云项目。Web 角色终结点几乎在部署后立即返回 HTTP 400 - 错误请求。当我签入跟踪消息日志时,我看到以下异常 -

Type : System.InvalidOperationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Custom counters file view is out of memory.
Source : System
Help link : 
Data : System.Collections.ListDictionaryInternal
TargetSite : Int32 CalculateMemory(Int32, Int32, Int32 ByRef)
HResult : -2146233079
Stack Trace :    at System.Diagnostics.SharedPerformanceCounter.CalculateMemory(Int32 oldOffset, Int32 totalSize, Int32& alignmentAdjustment)
   at System.Diagnostics.SharedPerformanceCounter.CreateCategory(CategoryEntry* lastCategoryPointer, Int32 instanceNameHashCode, String instanceName, PerformanceCounterInstanceLifetime lifetime)
   at System.Diagnostics.SharedPerformanceCounter.GetCounter(String counterName, String instanceName, Boolean enableReuse, PerformanceCounterInstanceLifetime lifetime)
   at System.Diagnostics.SharedPerformanceCounter..ctor(String catName, String counterName, String instanceName, PerformanceCounterInstanceLifetime lifetime)
   at System.Diagnostics.PerformanceCounter.InitializeImpl()
   at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, String instanceName, Boolean readOnly)

当 .NET 达到允许分配给性能计数器的内存量的上限时,似乎会导致此问题。

因此,我尝试在我的 Web.config 中使用以下条目增加内存分配 -

<configuration> 
<system.diagnostics> 
<performanceCounters filemappingsize="33554432" /> 
</system.diagnostics> 
</configuration> 

但即使这样,我仍然会遇到这个问题。有人可以给我一些解决问题的指示吗?

谢谢!

是否使用自己的性能计数器?我们在其中一个应用程序中也有同样的异常,该应用程序创建了性能计数器,但没有正确处理它们。

请注意,调用PerformanceCounter.Dispose()是不够的 - 它只是继承自Component.Dispose(),不添加其他功能。

因此,在释放多实例性能计数器的实例时,请始终调用PerformanceCounter.RemoveInstance(),否则性能计数器实例将增长,直到保留的内存(默认为 512 KB)已满。

示例模式如下所示(this.performanceCounters 是包含所用性能计数器的字典):

public void Dispose()
{
    this.Dispose(true);
    GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
    if (disposing)
    {
        if (this.performanceCounters != null)
        {
            foreach (PerformanceCounter counter in this.performanceCounters.Values)
            {
                counter.RemoveInstance();
                counter.Dispose();
            }
            this.performanceCounters = null;
        }
    }
}

来自本文档。

。如果在应用程序配置文件中定义大小,则仅当应用程序是导致性能计数器执行的第一个应用程序时,才会使用该大小。因此,指定文件映射大小值的正确位置是 Machine.config 文件。 ...

最新更新