c# - Why does this function work well on my main machine but not on a virtual one? (GetPhysicallyInstalledSystemMemory)
问题描述
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?
解决方案
Windows 通常会四舍五入总可用内存。您在 VM 下看到的结果可能仅仅是由于整数运算截断了两个除法的结果。
强制双算术至少除以一次1024.0
,看看错误是否仍然存在。
推荐阅读
- export - 在 Rstudio 中导出命令历史记录
- r - 通过 getGEO 加载 GSE 数据集在 R 中返回 0 行
- visual-c++ - 代码分析警告 C26476 和初始化 TASKDIALOGCONFIG 结构
- python - 线性回归模型形状 - ValueError:x 和 y 必须具有相同的第一维,但具有形状 (5,) 和 (1, 5)
- asp.net-core - Hangfire 执行两次作业
- reactjs - 如何在移动设备上将我的反应应用程序显示为桌面视图?
- javascript - 如何使用 eslint 在 FormattedMessage 组件中强制描述属性
- python - 当你运行一个 python 程序时会发生什么?
- flutter - 文本小部件中的 Flutter 共享首选项
- c++ - 如何创建自定义窗口作为孩子?