windows Win32/MFC:如何找到可用的空闲内存 (RAM)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2522943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:11:17  来源:igfitidea点击:

Win32/MFC: How to find free memory (RAM) available?

windowsmemorymfcwinapi

提问by user173438

Any suggestions/hints/links/tutorials would be appreciated! :)

任何建议/提示/链接/教程将不胜感激!:)

回答by Jerry Coffin

There really is no answer to this. Under normal circumstances, the OS will keep somethingin essentially all the memory on the system. Basically, once it's read something into memory, it'll keep a copy of it in memory until something else needs memory so the first one gets kicked out. There are a number of functions that can get you information about memory, but none of them even attempts to really return an amount of memory that's completely unused. The closest of which I'm aware is GlobalMemoryStatusEx, which does return a number for the amount of memory that's available.

这真的没有答案。在正常情况下,操作系统会在系统上的所有内存中保留一些东西。基本上,一旦将某些内容读入内存,它将在内存中保留一份副本,直到其他内容需要内存为止,因此第一个被踢出。有许多函数可以为您提供有关内存的信息,但它们甚至都没有尝试真正返回完全未使用的内存量。我所知道的最接近的是GlobalMemoryStatusEx,它确实返回可用内存量的数字。

That means whatever is currently in that memory is currently both in memory andon disk, so the copy in memory can be thrown away without having to write it to disk first. For example, if you ran a program, most of is code will stay in memory (until something else wants memory), in case you decide to run it again. Since it's just a copy of the program on disk, it can be thrown away, and (if necessary) reloaded from disk when needed.

这意味着当前在该内存中的任何内容当前都在内存磁盘上,因此可以丢弃内存中的副本而不必先将其写入磁盘。例如,如果您运行一个程序,大部分代码将保留在内存中(直到其他东西需要内存),以防您决定再次运行它。由于它只是磁盘上程序的副本,因此可以将其丢弃,并且(如有必要)在需要时从磁盘重新加载。

If you want more detail, you can use things like VirtualQueryExto get it -- but it'll usually overload you with information, telling you about each block of memory used in a given process, instead of giving a nice, simple number saying "x bytes free".

如果你想要更多的细节,你可以使用类似的东西VirtualQueryEx来获取它——但它通常会给你带来过多的信息,告诉你在给定进程中使用的每个内存块,而不是给出一个漂亮的简单数字,说“x字节免费”。

回答by SteelBytes

回答by Hans Passant

That's pretty easy to answer, free RAM is always sufficiently close to 0 to consider it zero and not bother. Unused RAM is always used by the file system cache, you can see this in the Taskmgr.exe, Performance tab.

这很容易回答,空闲 RAM 总是足够接近 0 以将其视为零而不打扰。未使用的 RAM 始终由文件系统缓存使用,您可以在 Taskmgr.exe 的“性能”选项卡中看到这一点。

If you actually mean "free virtual memory", the number you'd only really care about, then the answer is "not really possible". You'd need HeapWalk(), a very awkward and dangerous function to use. Only HeapWalk can detect blocks in the heap that are marked free but are still mapped. The number you'd arrive at is meaningless anyway. A program never runs out of free virtual memory blocks, it always runs out of large-enough memory blocks first.

如果您实际上是指“可用虚拟内存”,即您真正关心的数字,那么答案是“不太可能”。您需要使用 HeapWalk(),这是一个非常笨拙且危险的函数。只有 HeapWalk 可以检测堆中标记为空闲但仍被映射的块。无论如何,你得到的数字是没有意义的。程序永远不会用完可用的虚拟内存块,它总是先用完足够大的内存块。

Detecting this condition is easy enough. Malloc returns NULL, the new operator throws std::bad_alloc. Dealing with the condition is not easy. Solving it takes less than two hundred bucks, roughly the license fee for a 64-bit version of Windows.

检测这种情况很容易。Malloc 返回 NULL,新运算符抛出 std::bad_alloc。处理这种情况并不容易。解决它需要不到两百美元,大约是 64 位版本的 Windows 的许可费。