-
Notifications
You must be signed in to change notification settings - Fork 1
Sys_MemoryMonitor
SweerItTer edited this page Feb 1, 2026
·
3 revisions
MemoryMonitor 是 utilsCore Sys 模块的核心类,提供内存使用率监控功能,继承自 ResourceMonitor。
- 监控内存使用率
- 自动暂停和恢复监控
- 输出监控数据到文件
- 线程安全访问
- 系统性能监控
- 内存泄漏检测
- 负载均衡
- 资源调度
- 依赖: Linux /proc 文件系统, ResourceMonitor
- 被依赖: VisionPipeline, RecordPipeline 等模块
MemoryMonitor 是内存监控的封装类,提供:
- 内存使用率监控
- 继承自 ResourceMonitor
- 自动暂停和恢复机制
- 数据输出到 /tmp/memory_usage
- 继承: 继承自 ResourceMonitor
- 模板方法: 实现 sampleUsage() 纯虚函数
MemoryMonitor(int sleeptime = 1000);参数说明:
-
sleeptime(输入): 轮询间隔(毫秒),默认 1000
返回值: 无
所有权归属:
- MemoryMonitor 拥有内部资源的所有权
注意事项:
- 默认输出到 /tmp/memory_usage
- 自动启动监控线程
- sleeptime 控制采样频率
使用例程:
// 创建监控器(默认每 1000ms 采样一次)
MemoryMonitor memory_monitor;
// 自定义轮询间隔(每 500ms 采样一次)
MemoryMonitor memory_monitor(500);float getUsage();参数说明: 无
返回值: 内存使用率(0.0 - 100.0)
所有权归属:
- 只读访问
注意事项:
- 继承自 ResourceMonitor
- 线程安全操作
- 使用率 = (total - free) / total * 100%
- 自动唤醒暂停的监控线程
使用例程:
MemoryMonitor memory_monitor;
// 获取使用率
float usage = memory_monitor.getUsage();
printf("Memory Usage: %.1f%%\n", usage);bool sampleUsage(float& usage) override;参数说明:
-
usage(输出): 采样到的内存使用率
返回值:
-
true: 采样成功 -
false: 采样失败
所有权归属:
- 无所有权转移
注意事项:
- 实现 ResourceMonitor 的纯虚函数
- 读取 /proc/meminfo 文件
- 使用率 = (total - free) / total * 100%
- 包含 buffers 和 cached 内存
std::ifstream file("/proc/meminfo");
unsigned long long total = 0, free = 0;
std::set<std::string> needed = {"MemTotal:", "MemFree:"};
while (std::getline(file, line) && !needed.empty()) {
std::istringstream iss(line);
std::string key;
unsigned long long value;
iss >> key >> value;
if (key == "MemTotal:") {
total = value;
needed.erase(key);
} else if (key == "MemFree:") {
free = value;
needed.erase(key);
}
}说明:
- 读取 MemTotal 和 MemFree
- 使用率 = (total - free) / total * 100%
- 包含 buffers 和 cached 内存
- 继承自 ResourceMonitor 的同步机制
- getUsage() 是线程安全的
- sampleUsage() 在监控线程中调用
- 可以并发调用 getUsage()
- 自动暂停和恢复机制由 ResourceMonitor 管理
MemoryMonitor memory_monitor;
// 获取使用率
float usage = memory_monitor.getUsage();
printf("Memory Usage: %.1f%%\n", usage);MemoryMonitor memory_monitor;
while (running) {
float usage = memory_monitor.getUsage();
printf("Memory Usage: %.1f%%\n", usage);
std::this_thread::sleep_for(std::chrono::seconds(1));
}// 每 500ms 采样一次
MemoryMonitor memory_monitor(500);
while (running) {
float usage = memory_monitor.getUsage();
printf("Memory Usage: %.1f%%\n", usage);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}MemoryMonitor memory_monitor;
// 获取使用率
float usage1 = memory_monitor.getUsage();
// 等待 30 秒(监控线程会自动暂停)
std::this_thread::sleep_for(std::chrono::seconds(30));
// 再次获取使用率(线程会自动恢复)
float usage2 = memory_monitor.getUsage();MemoryMonitor memory_monitor;
// 数据会自动写入到 /tmp/memory_usage
// 每行一个值,格式: 65.2
// 读取文件内容
std::ifstream file("/tmp/memory_usage");
std::string line;
while (std::getline(file, line)) {
printf("Memory Usage: %s%%\n", line.c_str());
}- 输出文件: 数据自动写入到 /tmp/memory_usage
- 使用率计算: 使用率 = (total - free) / total * 100%
- 包含缓存: 包含 buffers 和 cached 内存
- 自动暂停: 30 秒无访问自动暂停监控线程
- 自动恢复: 调用 getUsage() 时自动恢复
- 线程安全: getUsage() 是线程安全的
- 轮询间隔: 构造时指定轮询间隔(默认 1000ms)
- /proc/meminfo: 依赖 /proc/meminfo 文件
- Base - 资源监控基类
- CpuMonitor - CPU 监控
- Sys 模块总览
主页
API 文档
DMA 模块
DRM 模块
- DRM 模块总览
- DeviceController - DRM 设备控制器
- DrmLayer - DRM 图层管理
- PlanesCompositor - DRM 平面合成器
- DrmBpp - DRM 格式定义
NET 模块
- NET 模块总览
- TcpServer - TCP 服务器
- SocketConnection - Socket 连接管理
- CommandHandler - 命令处理器
- DataPacket - 数据包
V4L2 模块
- V4L2 模块总览
- CameraController - V4L2 摄像头控制器
- Frame - V4L2 帧数据结构
- FormatTool - V4L2 格式工具
- Exception - V4L2 异常类
V4L2Param 模块
- V4L2Param 模块总览
- ParamControl - 参数控制
- ParamLogger - 参数日志
- ParamProcessor - 参数处理器
RGA 模块
- RGA 模块总览
- RgaConverter - RGA 转换器
- RgaProcessor - RGA 处理器
- FormatTool - RGA 格式工具
MPP 模块
- MPP 模块总览
- EncoderContext - 编码器上下文
- EncoderCore - 编码器核心
- JpegEncoder - JPEG 编码器
- StreamWriter - 流写入器
- MppResourceGuard - MPP 资源守护
- FileTools - 文件工具
- FormatTool - 格式工具
Sys 模块
- Sys 模块总览
- CpuMonitor - CPU 监控器
- MemoryMonitor - 内存监控器
- Base - 基础类
Mouse 模块
- Mouse 模块总览
- Watcher - 鼠标监视器
Utils 模块
- Utils 模块总览
- AsyncThreadPool - 异步线程池
- ConcurrentQueue - 并发队列
- FdWrapper - 文件描述符包装器
- FenceWatcher - 围栏监视器
- FixedSizePool - 固定大小对象池
- Logger - 日志记录器
- ObjectsPool - 对象池
- OrderedQueue - 有序队列
- ProgressBar - 进度条
- SafeQueue - 安全队列
- SharedBufferState - 共享缓冲区状态
- SimpleVariant - 简单变体类型
- ThreadPauser - 线程暂停器
- ThreadUtils - 线程工具
- Types - 类型定义
- UdevMonitor - Udev 监视器