我有一个发出内存请求的 c 程序。运行我的程序后,我使用 free -g -t 命令查看使用的内存。程序调用了malloc函数,但没有调用free函数。例如: 指针=malloc(900000000*sizeof(double)) 我认为那个调用会占用 ram 内存,但是当我使用 free 命令时,空闲内存不会改变。大约 6 兆。我多次运行该程序。该程序不释放内存,但空闲内存不会改变。 我认为空闲内存操作由 c 中的程序员负责。 我的程序比那个复杂一些,但是主要的操作是这样的。
我不明白为什么空闲(和占用)内存没有改变。
最佳答案
正如 Jonathan Leffler 在他的评论中所说,malloc() 实际上并未在物理 内存中分配您请求的内存,而是在您使用 内存时首先发生。如果您写入从 malloc() 获得的内存,那么您将看到“free”所见的内存使用量会增加。
来自 malloc man page在备注下:
By default, Linux follows an optimistic memory allocation strategy. This means that when malloc() returns non-NULL there is no guarantee that the memory really is available.
“free”报告物理 内存使用情况,如free man page 中所述
关于c - 内存请求 Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46144708/