.net 内存使用百分比

.net 内存使用百分比,.net,memory-management,.net,Memory Management,如何显示windows XP和7(.net2)上大多数工作的总内存使用率百分比 我尝试了以下解决方案,但没有成功(崩溃或冻结) 和使用 System.GC.GetTotalMemory(true); 工作样本感谢Darin [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServi

如何显示windows XP和7(.net2)上大多数工作的总内存使用率百分比

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

和使用

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

如果要使用
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,另一种可能是使用该类(您只需要添加对
Microsoft.VisualBasic
程序集的引用):


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

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

如果要使用
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,另一种可能是使用该类(您只需要添加对
Microsoft.VisualBasic
程序集的引用):


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

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

为什么没有成功?您得到了什么样的输出?您期望的输出是什么?不幸的是,这里没有水晶球:)什么是“没有成功”呢?解决方案1在windows 7上工作正常,windows XP冻结,解决方案2没有显示正确的百分比为什么没有成功?您得到了什么样的输出?您期望的输出是什么?不幸的是,这里没有水晶球:)什么是“不成功”呢?解决方案1在windows 7上工作正常,windows XP冻结,解决方案2没有显示正确的百分比我喜欢Interop,它工作正常。谢谢你的输入。我正在考虑向我们的应用程序中添加类似的内容,以便在开发过程中跟踪内存使用情况,并在内存突然增加时显示仅调试的消息。已经有了类似的SQL查询时间跟踪。我很喜欢Interop,它工作得很好。谢谢你的输入。我正在考虑向我们的应用程序中添加类似的内容,以便在开发过程中跟踪内存使用情况,并在内存突然增加时显示仅调试的消息。已经对SQL查询时间进行了类似的跟踪。