c++ - 为什么RES保持缓慢增长?
问题描述
我编写了一个简单的程序来观察 top 命令中的 VIRT 和 RES 变化。我很好奇为什么 RES 保持缓慢增长。
该程序在以下系统中运行:
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
...
这是程序代码:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
using namespace std;
int main(int argc, char* argv[]) {
srand(time(nullptr));
while (true) {
int num = rand();
int size = 0;
if (num % 3 == 0) {
size = rand() % (1024 * 1024 * 500) + 1;
} else if (num % 3 == 1) {
size = rand() % (1024 * 1024) + 1;
} else {
size = rand() % 1024 + 1;
}
char* mem = (char*)malloc(size);
cout << "malloc " << size << endl;
sleep(1);
int use = max(rand() % size, 1);
memset(mem, 1, use);
cout << "use " << use << endl;
sleep(1);
free(mem);
cout << "free " << size << endl;
sleep(1);
}
return 0;
}
VIRT RES(KB)
47476 33840
47476 33856
47476 33868
47476 33872
47476 33896
47476 33900
正如我们所见,RES(通话后免费)一直在缓慢增长,为什么?
另外:我担心它一直在增长,最终会耗尽系统的内存,尽管可能需要很多年。
解决方案
推荐阅读
- php - 如何连接多个mysql db
- sql - 有或没有 where 条件的查询结果相同
- javascript - 如何在保持纵横比而不切断画布的同时使 HTML 画布适合屏幕?
- python - Python Pandas:从单元格中提取数据并将其转换为列
- typescript - Typescript - 在指定特定字段值的同时定义基于其他类型的类型
- windows - 如何抑制或解决窗口找不到...确保正确输入名称
- css - 角度材料和materila-ui和Materializecss是否与材料设计有关?
- spring-boot - 组 main 中的调度程序工作人员中的 Azure BlobClient.beginCopy(..) 错误失败并出现未捕获的异常
- angular - Scullyio - 构建后如何在静态 Index.html 文件中呈现 Angular Material 主题(CSS)?
- python - 更新或创建不会更新,只会创建