linux下C程序查看内存剩余大小

嵌入式linux中很多内存资源都比较小,所以很多程序中在malloc时就需要提前知道内存剩余的大小

下面是一种调用 sysinfo接口来实现。

示例代码如下:

cpp 复制代码
#include <stdio.h>
#include <linux/kernel.h>
#include <linux/unistd.h>
#include <sys/sysinfo.h>
 
int main(void)
{
    struct sysinfo s_info;
    int error = sysinfo(&s_info);
    printf("error0: %d, total: %lu free: %lu \n", error, s_info.totalram, s_info.freeram);
    //func_call1(pcModelName);
 
    error = sysinfo(&s_info);
    printf("error1: %d, total: %lu free: %lu \n", error, s_info.totalram, s_info.freeram);
 
    int msg[500];
    int i = 0;
    for(i = 0; i < 1; i++)
    {
        //func_call2(pcSrcFile, msg);
        error = sysinfo(&s_info);
        printf("error2: %d, total: %lu free: %lu \n", error, s_info.totalram, s_info.freeram);
 
        //func_call3(pcSrcFile, msg);
        error = sysinfo(&s_info);
        printf("error3: %d, total: %lu free: %lu \n", error, s_info.totalram, s_info.freeram);
    }
 
    //func_call4();
    error = sysinfo(&s_info);
    printf("error4: %d, total: %lu free: %lu \n", error, s_info.totalram, s_info.freeram);
}
相关推荐
杨云龙UP13 小时前
ODA-Oracle实践记录:通过Navicat导出导入表CSV文件并清理重复数据,处理OA待办延迟刷新问题_20260416
linux·运维·服务器·数据库·sql·mysql·oracle
Oliver_LaVine13 小时前
java项目启动报错:CreateProcess error=206, 文件名或扩展名太长
java·linux·jenkins
D4c-lovetrain13 小时前
Linux个人心得27 (redis哨兵模式实战)
linux·redis·bootstrap
孬甭_13 小时前
字符函数及字符串函数
c语言·开发语言
楼田莉子14 小时前
Linux网络:IP协议
linux·服务器·网络·c++·学习·tcp/ip
网域小星球14 小时前
C 语言从 0 入门(二十五)|位运算与位段:底层开发、嵌入式核心
c语言·开发语言
wuminyu14 小时前
专家视角看JVM_StartThread
java·linux·c语言·jvm·c++
敲上瘾14 小时前
高并发内存池(三):PageCache(页缓存)的实现
linux·c++·缓存·高并发内存池·池化技术
Ops菜鸟(Xu JieHao)14 小时前
Linux 内网远程桌面Xrdp ~保姆级教程
linux·运维·服务器·windows·远程桌面·远程·xrdp
zjeweler14 小时前
linux服务器部署openclaw最新最细教程(非docker版)
linux·服务器·docker·openclaw