Home TotalVirtualMemory returns unrealistic number for my application
Reply: 1

TotalVirtualMemory returns unrealistic number for my application

user8114848
1#
user8114848 Published in 2017-12-07 16:34:37Z

My application is returning a very large number for TotalVirtualMemory. I instantiate a ComputerInfo() object and call TotalVirtualMemory and the value returned is ~130TB. Creating a basic project to test this seems to return the correct value: ~4GB. What could be causing this? My Code is below:

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;
        }
    }
}
Scott Chamberlain
2#
Scott Chamberlain Reply to 2017-12-07 16:50:45Z

4GB is only expected for 32 bit processes, the number you got is the expected number for 64 bit processes

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

Change your "platform target" in the "project properties -> build tab", to be only run on x86 instead of being set to x64 or AnyCPU and you will see the 4GB number you expected.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.352036 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO