内存使用百分比

时间:2011-09-04 11:58:54

标签: .net memory-management

如何以Windows XP和7(.net2)上的大部分工作百分比来显示总内存使用量

我尝试了以下解决方案,没有成功(崩溃或冻结)

http://www.pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html

并使用

System.GC.GetTotalMemory(true);

工作样本感谢Darin,

[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public class MEMORYSTATUSEX
    {
        public uint dwLength = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(MEMORYSTATUSEX));
        public uint dwMemoryLoad;
        public ulong ullTotalPhys;
        public ulong ullAvailPhys;
        public ulong ullTotalPageFile;
        public ulong ullAvailPageFile;
        public ulong ullTotalVirtual;
        public ulong ullAvailVirtual;
        public ulong ullAvailExtendedVirtual;
    }
    [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
    static extern bool GlobalMemoryStatusEx(MEMORYSTATUSEX lpBuffer);

    private void timer1_Tick(object sender, EventArgs e)
    {
        var result = new MEMORYSTATUSEX();
        if (GlobalMemoryStatusEx(result))
            getpercentage(((double)result.ullTotalPhys - result.ullAvailPhys) / result.ullTotalPhys);
    }

    static void getpercentage(double ratio)
    {
        string usage = string.Format("usage {0:0%}", ratio);
        Console.WriteLine(usage);
    }

1 个答案:

答案 0 :(得分:5)

如果你想使用GlobalMemoryStatusEx方法,这里有一个例子:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class MEMORYSTATUSEX
{
    public uint dwLength;
    public uint dwMemoryLoad;
    public ulong ullTotalPhys;
    public ulong ullAvailPhys;
    public ulong ullTotalPageFile;
    public ulong ullAvailPageFile;
    public ulong ullTotalVirtual;
    public ulong ullAvailVirtual;
    public ulong ullAvailExtendedVirtual;
    public MEMORYSTATUSEX()
    {
        this.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
    }
}

class Program
{
    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);

    static void Main()
    {
        MEMORYSTATUSEX result = new MEMORYSTATUSEX();
        if (GlobalMemoryStatusEx(result))
        {
            Console.WriteLine(
                "{0}% memory left", 
                100.0 * result.ullAvailPhys / result.ullTotalPhys
            );
        }
    }
}

如果您不喜欢Interop,另一种可能性是使用ComputerInfo类(您只需要添加对Microsoft.VisualBasic程序集的引用):

class Program
{
    static void Main()
    {
        Microsoft.VisualBasic.Devices.ComputerInfo ci = new Microsoft.VisualBasic.Devices.ComputerInfo();
        Console.WriteLine(
            "{0}% memory left",
            100.0 * ci.AvailablePhysicalMemory / ci.TotalPhysicalMemory
        );
    }
}

另一种可能性是查询性能计数器以找出可用内存:

class Program
{
    static void Main()
    {
        // You could also use "Available Bytes", "Available KBytes", "Available MBytes"
        PerformanceCounter pc = new PerformanceCounter("Memory", "Available Bytes");
        Console.WriteLine(Convert.ToInt64(pc.NextValue()));
    }
}