c - 内存请求 Linux

标签 c linux memory memory-management

我有一个发出内存请求的 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/

相关文章:

c - 在 C 中打印 double 而不是整数

c - 数独:检查 3x3 网格中的重复值

c - strtok函数c解释

linux - 生成文件 : No rule to make target

使用数组初始化时的 C++ STL vector 内存管理?

c++ - 我可以从 C 或 C++ 外部控制 Chrome 扩展程序吗?

linux - 在 Linux bash 上自动将文件复制到 USB

PHP配置错误DBA : Could not find necessary header file(s)

objective-c - 创建 NSManagedObject(大尺寸)。内存警告和应用程序崩溃

python - 清除通过在 IPython 中绘图分配的内存