我的应用程序为TotalVirtualMemory返回了一个非常大的数字。我实例化一个ComputerInfo()对象并调用TotalVirtualMemory,返回的值是~130TB。创建一个基本项目来测试它似乎返回正确的值:~4GB。可能是什么导致了这个?我的代码如下:
using System;
using Microsoft.VisualBasic.Devices;
namespace ConsoleApp1
{
public class Program
{
private static void Main(string[] args)
{
var test = new Program();
Console.WriteLine($"Total Virtual Memory: {test.GetTotalVirtualMemory()}");
Console.WriteLine($"Total Virtual Memory Readable: {test.GetTotalVirtualMemory() * (1.0 / 1024.0 / 1024.0 / 1024.0)}");
}
public ulong GetTotalVirtualMemory()
{
return new ComputerInfo().TotalVirtualMemory;
}
}
}
答案 0 :(得分:3)
仅对32位进程预期4GB,您获得的数字是64位进程的预期数量
using System;
using Microsoft.VisualBasic.Devices;
namespace ConsoleApp1
{
public class Program
{
private static void Main(string[] args)
{
var test = new Program();
Console.WriteLine($"Is 64 Bit Process: {Environment.Is64BitProcess}");
Console.WriteLine($"Total Virtual Memory: {test.GetTotalVirtualMemory()}");
Console.WriteLine($"Total Virtual Memory Readable: {test.GetTotalVirtualMemory() * (1.0 / 1024.0 / 1024.0 / 1024.0)}");
}
public ulong GetTotalVirtualMemory()
{
return new ComputerInfo().TotalVirtualMemory;
}
}
}
更改您的平台目标"在"项目属性中 - >构建选项卡",只能在x86上运行而不是设置为x64或AnyCPU,你会看到你期望的4GB数字。