首页 > 解决方案 > 在控制上连续显示使用的 RAM

问题描述

我有一个过程

Process p = new Process();
p.StartInfo = new ProcessStartInfo("java", -jar test.jar);

我需要获取 RAM,实时process p使用CPU

我使用了获取 RAM 的代码,但不是实时的:

while(true) {
    txtRamIsUsed.Text = BytesToReadableValue(p.PrivateMemorySize64);
}
public string BytesToReadableValue(long number)
{
    List<string> suffixes = new List<string> { " B", " KB", " MB", " GB", " TB", " PB" };

    for (int i = 0; i < suffixes.Count; i++)
    {
        long temp = number / (int)Math.Pow(1024, i + 1);

        if (temp == 0)
        {
            return (number / (int)Math.Pow(1024, i)) + suffixes[i];
        }
    }

    return number.ToString();
}

标签: c#winforms

解决方案


推荐阅读