FreeMemory 和 CPUUsage 导致内存泄漏和系统内存不足

本文关键字:泄漏 系统 内存不足 内存 CPUUsage FreeMemory | 更新日期: 2023-09-27 18:24:38

private float FreeMemory
{
    get { return _nFreeMemory; }
    set
    {
        _nFreeMemory = value;
        statusBarPanelMem.Text = _nFreeMemory + " MB prosto";
    }
}
private int CPUUsage
{
    get { return _nCpuUsage; }
    set
    {
        _nCpuUsage = value;
        statusBarPanelCPU.Text = "CPU poraba " + _nCpuUsage + "%";
    }
}

然后我正在使用计时器并调用

private void InitialiseCounterRamNetwork()
{
    _cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
    _ramCounter = new PerformanceCounter("Memory", "Available MBytes", true);
}
private void timerMemProcNetwork_Tick(object sender, EventArgs e)
{
   FreeMemory = _ramCounter.NextValue();
   CPUUsage = Convert.ToInt32(_cpuCounter.NextValue());
}

但是由于这种情况,我得到了内存泄漏和系统内存不足异常。为什么GC不收集这个?

FreeMemory 和 CPUUsage 导致内存泄漏和系统内存不足

不可重现 (Fx 4.5(。

列出的代码不会导致任何内存泄漏。