sysinfo为freeram返回错误值(即使使用mem_unit) [英] sysinfo returns incorrect value for freeram (even with mem_unit)

查看:117
本文介绍了sysinfo为freeram返回错误值(即使使用mem_unit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的完整C MATE小程序可以在github的此处找到: https://github.com/geniass/mate-resource-applet/tree/cmake (分支CMAKE).现在这是一堆乱七八糟的东西,所以请看下面的代码.

My full C MATE applet can be found at github here: https://github.com/geniass/mate-resource-applet/tree/cmake (BRANCH CMAKE). It's a hacky mess right now, so see the code below.

我找不到小程序来显示计算机的免费ram,所以基本上就是这样.我正在使用sysinfo来获取此信息,并且它对于系统的总RAM正常工作(大约4GB,显示3954 MB).htop 显示使用了 3157 MB,其中 3954 MB.

I couldn't find an applet to display my computer's free ram, so that's basically what this is. I am using sysinfo to get this information, and it works fine for my system's total ram (roughly 4GB, it shows 3954 MB). htop shows 3157 MB used of 3954 MB.

但是,sysinfo为免费ram提供的值(136 MB)显然是错误的(如果免费ram是尚未分配的ram或其他东西,我不知道).

However, the value sysinfo gives for free ram (136 MB) is obviously wrong (if free ram is ram that hasn't been allocated or something, I don't know).

这个问题是相同的问题,但是涉及mem_unit的解决方案不起作用,因为我系统上的mem_unit = 1.

This question is the same problem, but the solution, involving mem_unit, doesn't work because mem_unit = 1 on my system.

这是一个提供相同值的最小程序:

Here's a minimal program that gives the same values:

#include <stdio.h>
#include <sys/sysinfo.h>


int main() {
    /* Conversion constants. */
    const long minute = 60;
    const long hour = minute * 60;
    const long day = hour * 24;
    const double megabyte = 1024 * 1024;
    /* Obtain system statistics. */
    struct sysinfo si;
    sysinfo (&si);
    /* Summarize interesting values. */
    printf ("system uptime : %ld days, %ld:%02ld:%02ld\n", 
            si.uptime / day, (si.uptime % day) / hour, 
            (si.uptime % hour) / minute, si.uptime % minute);
    printf ("total RAM   : %5.1f MB\n", si.totalram / megabyte);
    printf ("free RAM   : %5.1f MB\n", si.freeram / megabyte);
    printf ("mem_unit:   : %u\n", si.mem_unit);
    printf ("process count : %d\n", si.procs);
    return 0;
}

输出:

system uptime : 0 days, 10:25:18
total RAM   : 3953.9 MB
free RAM   : 162.1 MB
mem_unit:   : 1
process count : 531

这是怎么回事?Freeram不是我想的吗?

What's going on here? Is freeram not what I think it is?

推荐答案

在不使用 free 内存的情况下,Linux不喜欢Linux.因此,当有可用的可用内存时,它会暂时将其用作 cache 内存和缓冲区.可用内存似乎很低,但是只要程序需要内存,Linux就会减少其缓存/缓冲区使用率,并为程序提供所需的内容.

Linux doesn't like when free memory is used nowhere. So when there is free memory available, it takes it temporarily as cache memory and buffers. The free memory seems very low, but as soon as a program requires memory, Linux reduces its cache / buffers usage and give the program what it wants.

Linux命令 free -m 显示内存,高速缓存和缓冲区的状态.

The Linux command free -m displays the state of the memory, cache and buffers.

请参见此链接示例和详细信息.

See this link for example and detailed information.

这篇关于sysinfo为freeram返回错误值(即使使用mem_unit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆