Pow*_*fet 0 .net memory-management
如何在Windows XP和7(.net2)上以大多数工作的百分比来显示总内存使用量
我尝试了以下解决方案没有成功(崩溃或冻结)
http://www.pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html
和使用
System.GC.GetTotalMemory(true);
Run Code Online (Sandbox Code Playgroud)
工作样本感谢达林,
[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);
}
Run Code Online (Sandbox Code Playgroud)
如果你想使用这个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
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不喜欢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
);
}
}
Run Code Online (Sandbox Code Playgroud)
另一种可能性是查询性能计数器以找出可用内存:
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()));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
915 次 |
| 最近记录: |