c# - 总虚拟内存为我的应用程序返回不切实际的数字

标签 c# .net

我的应用程序为 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;
        }
    }
}

最佳答案

4GB 仅适用于 32 位进程,您得到的数字是 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 数字。

enter image description here

关于c# - 总虚拟内存为我的应用程序返回不切实际的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47699624/

相关文章:

c# - WPF TreeViewItem 切换按钮可见性

c# - 公共(public)基类初始化

c# - HTML 选择器库

c# - 如何组合||条件语句中的运算符

c# - 无法使用此程序绘制图像?

.net - 使用 Kestrel 和 .NET 核心中间件压缩 HTTP 响应

c# - 有什么方法可以限制 Unity3D 中面板的触摸输入吗?

c# - TimeZoneInfo.ConvertTimeFromUtc 没有将旧日期转换为夏令时

c# - 为什么我的选择排序算法始终比插入排序算法快?

c# - 为什么在从被扩展类的方法中调用扩展方法时必须显式指定 "this"?