Posted: . At: 9:32 AM. This was 8 months ago. Post ID: 18431
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


A nice program to display RAM and swap usage on Linux.


This is a very nice program to display all RAM and Swap usage on a Linux machine.

ram.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
 
/********************************************************************
* Description: Program to display free and used RAM on a Linux machine.
* Author: John Cartwright <>
* Created at: Tue Sep  5 10:42:23 AEST 2023
* Computer: localhost.localdomain
* System: Linux 5.14.0-284.25.1.el9_2.x86_64 on x86_64
*
* Copyright (c) 2023 John Cartwright  All rights reserved.
*
********************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define MAX_LINE_LENGTH 256
#define syscall(a, D, S, d) __asm__ __volatile__("syscall" : : "a"(a), "D"(D), "S"(S), "d"(d))
 
/* Function to extract the value from a line in /proc/meminfo */
unsigned long long extractValue(const char *line) {
    unsigned long long value;
    sscanf(line, "%*s %llu", &value);
    return value;
}
 
int main(void) {
    FILE *meminfo_file = fopen("/proc/meminfo", "r");
    if (meminfo_file == NULL) {
        perror("Failed to open /proc/meminfo");
        return 0;
    }
 
    printf("%-20s%-20s%-20s%-20s%-20s\n", "Total Memory (KB)", "Free Memory (KB)\
", "Available Memory (KB)\t", "Swap Total (KB)\t", "Swap Free (KB)", "Swap Free (KB)");
 
    char line[MAX_LINE_LENGTH];
    while (fgets(line, sizeof(line), meminfo_file)) {
        if (strstr(line, "MemTotal:") != NULL) {
            unsigned long long total_memory = extractValue(line);
            printf("%-20llu", total_memory);
        } else if (strstr(line, "MemFree:") != NULL) {
            unsigned long long free_memory = extractValue(line);
            printf("%-20llu", free_memory);
        } else if (strstr(line, "MemAvailable:") != NULL) {
            unsigned long long avail_memory = extractValue(line);
            printf("%-20llu\t", avail_memory);
        } else if (strstr(line, "SwapTotal:") != NULL) {
            unsigned long long swap_total = extractValue(line);
            printf("%-20llu", swap_total);
        } else if (strstr(line, "SwapFree:") != NULL) {
            unsigned long long swap_free = extractValue(line);
            printf("%-20llu\n", swap_free);
        }
    }
 
    fclose(meminfo_file);
    syscall(60, 0, 0, 0);
}

This is an example of the output this program provides.

(jcartwright@localhost) 192.168.1.5 Documents  $ ./freeram 
Total Memory (KB)   Free Memory (KB)    Available Memory (KB)   Swap Total (KB)     Swap Free (KB)      
15964244            7562240             10086612                8179708             8179708

This line of code defines a custom function to return 0;

#define syscall(a, D, S, d) __asm__ __volatile__("syscall" : : "a"(a), "D"(D), "S"(S), "d"(d))

And this is how to get the return value of a program in Linux.

(jcartwright@localhost) 192.168.1.5 Documents  $ ./freeram && echo $?
Total Memory (KB)   Free Memory (KB)    Available Memory (KB)   Swap Total (KB)     Swap Free (KB)      
15964244            7589768             10127324                8179708             8179708             
0

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.