2011-09-04 97 views
0

如何dispay百分比最多的工作总内存使用在Windows XP和7(.NET2)内存使用率

我曾尝试以下解决方案,而更迭(崩溃或冻结)

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

,并使用

System.GC.GetTotalMemory(true); 

工作样本感谢到达林,

[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

“没有成功”是什么意思? –

+0

解决方案1在Windows 7上工作正常,Windows XP冻结,解决方案2不显示正确的百分比 –

回答

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

,如果你不喜欢互操作的另一种可能性是使用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 
     ); 
    } 
} 

然而ANOT她的可能性是查询性能计数器以找出可用内存:

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

我喜欢Interop,它的工作正常。谢谢你的输入 –

+0

不错。我正在考虑在开发过程中使用我们的应用程序来追踪内存使用情况,并在突然增加时显示仅限调试消息。已经有类似的SQL查询时间跟踪。 – Dennis