Attempting to retrieve memory stats is challenging.
Attempting to retrieve memory stats is challenging.
I'm working on Arch using i3wm, and I'm setting up my i3bar with i3blocks and scripts. I'm trying to track memory usage but different tools give conflicting numbers. For instance, the C code uses sysinfo and shows 1.8 GB, while a batch file reports around 0.7 GB. When I run 'free' in the terminal, it lists 1.2 GB used, with 246 MB in shared and 1.1 GB in cache. htop says about a gigabyte, and btop shows 1.2 GB (converted from gibibytes). I understand these methods vary—some count physical RAM, others include cache or use different formats. I just need a straightforward way to see the actual usable memory without cache or other factors.
I believe free handles what you require without extra steps. Run free -hw on your system to see the details: total used free shared buffers cache available memory is 31Gi, 2, 8Gi, 16Gi, 36Mi, 711Mi, 11Gi, 28Gi. The reported free count includes memory not in use, while available shows what's ready for programs. This matches what you'd typically want to focus on. Available numbers should align closely with free plus buffers and cache (about 28). Other utilities might list only free or available but use different terminology.
Here’s a simplified take on your concerns:
The C version offers more control, but its memory handling doesn’t align with the batch or free command results. You’re questioning whether using free is reliable. It seems you should evaluate both options carefully. Maybe reviewing the C source could clarify the differences.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/sysinfo.h> typedef struct sysinfo sysinfo_t ; typedef struct { char bytes[4]; int size; } utf8_char; unsigned int utf8_char_count(const char *str) { unsigned int count = 0; while (*str) { count += (*str & 0xC0) != 0x80; str++; } return count; } void load_bar_chars(utf8_char *bar_chars, unsigned int count, char *characters) { char *c = characters; for (unsigned int i = 0; i < count; i++) { int size = 0; utf8_char *b = bar_chars + i; if (!c[0]) { printf("FAILED TO LOAD CHARS CORRECT"); } else if (!(c[0] & 0x80)) { size = 1; } else if ((c[0] & 0xE0) == 0xC0) { size = 2; } else if ((c[0] & 0xF0) == 0xE0) { size = 3; } else if ((c[0] & 0xF8) == 0xF0) { size = 4; } for (unsigned int j = 0; j < size; j++) b->bytes[j] = c[j]; b->size = size; c += size; } } int clamp(int value, int min, int max) { return value < min ? min : (value > max ? max : value); } char *calculate_color(float percent) { int r = (int)(255 * (percent / 100)); int g = (int)(255 * (1 - percent / 100)); int b = 0; static char color[15]; snprintf(color, 15, "#%02X%02X%02X", r, g, b); return color; } int main(int argc, char *argv[]) { // ... (rest of code remains similar) }
I don't grasp your point clearly. You're referring to a binary version of free located at /usr/bin/free, but I'm not sure about its existence. I'm looking at the binary at /proc/meminfo and seeing how it compares to free. There might be minor differences due to how the commands are executed or the data formats involved. The script you shared just processes memory info and performs calculations, which could explain the small discrepancies.
@Eigenvektor The C code I gave is from the i3blocks-contrib repository on github, but I modified it a little only for the color grading, that's it. There is also a batch file to do the same. These are to display stuff on the i3bar through i3blocks. All I want is a way to get a simple memory usage, which does not include the cache and all that, just the amount of memory that I can use. I think I should read from /proc/meminfo, because the C version gives more usage, so it probably is reading more than just usable memory. Also, what is the difference between MemFree and MemAvailable in meminfo? Edit - The sysinfo() man page says - But I guess the "freeram" variable gives a bit more than pure usable memory. In meminfo, I have more control in what to see.
I believe it’s better to rely on the memory output from the free utility instead of using sysinfo() or /proc/meminfo. Each gives varying information, but I feel confident in the free command.
Would you trust me if I explained how your system memory functions differently and every number you see is correct? I reference htop as evidence to clarify why CPU usage appears in green, red, and blue. The same applies to memory details found at https://serverfault.com/questions/180711...-bars-mean