开发者

Memory usage percentage

开发者 https://www.devze.com 2023-04-02 02:32 出处:网络
how to dispay total memory usage in percentage most work on windows XP and 7 (.net2) I have tried the following solutions without succes (crashes or freezes)

how to dispay total memory usage in percentage most work on windows XP and 7 (.net2)

I have tried the following solutions without succes (crashes or freezes)

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

and using

System.GC.GetTotalMemory(true);

working sample thank to 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;
    }
    [Syste开发者_如何学JAVAm.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);
    }


If you want to use the GlobalMemoryStatusEx method, here's an example:

[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
            );
        }
    }
}

Another possibility if you don't like Interop is to use the ComputerInfo class (you just need to add reference to the Microsoft.VisualBasic assembly):

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
        );
    }
}

And yet another possibility is to query the performance counters to find out the available memory:

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()));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消