Why does this function work well on my main machine but not on a virtual one? (GetPhysicallyInstalledSystemMemory)

Viewed 245

I have the following code that (correctly) gives me the total installed memory on my computer (note, not the total physical memory, which would be a little less than the installed memory):

using System.Runtime.InteropServices;

        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetPhysicallyInstalledSystemMemory(out long TotalMemoryInKilobytes);

        public float getInstalledRAM()
        {
            long memKb;
            GetPhysicallyInstalledSystemMemory(out memKb);
            return float.Parse((memKb / 1024 / 1024).ToString());
        }

However, when I run it on my test virtual machine, it gives me 1GB less than it should (don't know if the amount matters, but bottom line it's giving me a wrong value). Any possible causes for this?

1 Answers

Windows normally rounds the total available memory. The result you are seeing under the VM might simply be due to integer arithmetics truncating the result of the two divisions.

Force double arithmetics dividing at least once by 1024.0 and see if the error persists.

Related