Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何修复PerformanceCounterCategory.GetInstanceName的OutOfMemoryException_C#_.net_Performancecounter - Fatal编程技术网

C# 如何修复PerformanceCounterCategory.GetInstanceName的OutOfMemoryException

C# 如何修复PerformanceCounterCategory.GetInstanceName的OutOfMemoryException,c#,.net,performancecounter,C#,.net,Performancecounter,如果我执行这个代码 PerformanceCounterCategory cat = new PerformanceCounterCategory("Process"); var instances = cat.GetInstanceNames(); // OutOfMemoryException 我得到一个OutOfMemoryException完整堆栈跟踪是 A first chance exception of type 'System.OutOfMemoryException' occ

如果我执行这个代码

PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
var instances = cat.GetInstanceNames(); // OutOfMemoryException
我得到一个
OutOfMemoryException
完整堆栈跟踪是

A first chance exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll
System.OutOfMemoryException: An Exception of Type "System.OutOfMemoryException" occured.
   at Microsoft.Win32.RegistryKey.InternalGetValue(String name, Object defaultValue, Boolean doNotExpand, Boolean checkSecurity)
   at Microsoft.Win32.RegistryKey.GetValue(String name)
   at System.Diagnostics.PerformanceMonitor.GetData(String item)
   at System.Diagnostics.PerformanceCounterLib.GetPerformanceData(String item)
   at System.Diagnostics.PerformanceCounterLib.get_CategoryTable()
   at System.Diagnostics.PerformanceCounterLib.GetCategorySample(String machine, String category)
   at System.Diagnostics.PerformanceCounterCategory.GetCounterInstances(String categoryName, String machineName)
   at WindowsFormsApplication5.Form1.button1_Click(Object sender, EventArgs e)
只有在x86下运行相同的代码时,才会在x64 exe中发生这种情况。有什么办法解决这个问题吗


操作系统是Windows 8.1 x64,带有8GB内存(这不是物理内存问题)。

我最终通过一篇知识库文章解决了这个问题

  • 备份
    Perfc009.dat
    Perfh009.dat
    %Systemroot%\System32
  • 将文件从安装介质复制到%Systemroot%\System32。虽然这篇kb文章是针对server 2008的,我使用的是windows 8.1,但我使用7-zip从安装dvd的文件
    sources\install.wim
    (windows 8 Pro的文件夹
    2\windows\System32\
    )中提取了文件
  • 在注册表中,将最后一个计数器值更改为1846(十进制),并将最后一个帮助值更改为1847(十进制)
  • 从性能子项中删除以下值(如果存在):
    First Counter、First Help、Last Counter、Last Help
  • 使用
    lodctr/R
    (我在System32和Syswow64文件夹中的admin cmd提示符下做了一次,只是为了确保)重新启用第三方计数器
  • 文章还建议,如果任何.NET应用程序无法启动,您必须重新安装创建性能计数器的.NET程序集,例如
    InstallUtil/i Microsoft.Mom.ConfigService.dll
    ,但在我的情况下,这不是必需的

  • 哪个windows版本?在问题调用之前,您有多少可用内存?@tolanj我添加了一些详细信息“操作系统是windows 8.1 x64,具有8GB RAM(这不是物理内存问题)。=>是的,但“真正”的问题可能在代码的其他地方,即您调用cat.GetInstanceNames()时内存不足;但我想确认一下,在打那个电话之前,你有足够的空间,问题不在于其他地方的内存使用异常。因此,您可以在调用之前明确确认可用内存。另外,HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib\009注册表项和C:\Windows\System32\perfc009.dat文件(如果其较大,则为文本文件,仅发布sze)的内容对您有好处。通常,您只需运行
    lodctr/R
    命令(不执行任何其他步骤)就可以逃脱。嗯,我不买。您是否忘记列出步骤6a。重新启动机器?@500 InternalServerError-在我的情况下不是这样,我还尝试了
    lodctr/q
    loadctr/d:
    一些第三方性能计数器@HansPassant-Noop,这对我来说是有效的,没有重新启动,甚至没有重新启动任何与性能计数器相关的服务。