【问题标题】:Custom counters file view is out of memory自定义计数器文件视图内存不足
【发布时间】:2013-09-21 23:38:57
【问题描述】:

我有一个带有一个 Web 角色的 Azure 云项目。 Web 角色端点在部署后几乎立即返回 HTTP 400 - Bad Request。当我检查跟踪消息日志时,我看到以下异常 -

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> 

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

谢谢!

【问题讨论】:

    标签: c# wcf azure performancecounter


    【解决方案1】:

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

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

    因此,在处理多实例 PerformanceCounter 的实例时,请始终调用 PerformanceCounter.RemoveInstance(),否则您的 PerformanceCounter 实例将不断增长,直到预留内存(默认为 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;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      来自this 文档。

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

      【讨论】:

      • 这确实有效。谢谢!除了我的网络角色,我不确定哪个其他 .NET 进程可以成为第一个创建者?
      猜你喜欢
      • 2011-06-16
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多