List methods in C for get total, cached and buffered RAM from Linux kernel.
- User
List methods in C for get total, cached and buffered RAM from Linux kernel.
- Assistant
List methods in C for get total, cached and buffered RAM from Linux kernel.
In C, you can retrieve information about total, cached, and buffered RAM in a Linux system primarily by reading from the /proc/meminfo file, which provides a detailed summary of memory usage. Below are common methods to get this information:
The simplest way to gather memory information is to read the /proc/meminfo file. This can be done using standard file I/O operations in C.
Here is an example program that demonstrates how to read total, cached, and buffered RAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void get_memory_info() {
FILE* file = fopen("/proc/meminfo", "r");
if (file == NULL) {
perror("Could not open /proc/meminfo");
return;
}
char line[256];
long total_ram = 0;
long cached_ram = 0;
long buffered_ram = 0;
while (fgets(line, sizeof(line), file)) {
if (sscanf(line, "MemTotal: %ld kB", &total_ram) == 1) {
// Found total RAM
} else if (sscanf(line, "Cached: %ld kB", &cached_ram) == 1) {
// Found cached RAM
} else if (sscanf(line, "Buffers: %ld kB", &buffered_ram) == 1) {
// Found buffered RAM
}
}
fclose(file);
printf("Total RAM: %ld kB\n", total_ram);
printf("Cached RAM: %ld kB\n", cached_ram);
printf("Buffered RAM: %ld kB\n", buffered_ram);
}
int main() {
get_memory_info();
return 0;
}
The sysconf function provides some memory-related information on POSIX systems, but it does not provide specific metrics like cached or buffered memory. It can be used to get some general configurations like page size, which indirectly relates to memory.
Here’s a limited example using sysconf:
#include <stdio.h>
#include <unistd.h>
int main() {
long pages = sysconf(_SC_PHYS_PAGES);
long page_size = sysconf(_SC_PAGE_SIZE);
long total_memory = pages * page_size;
printf("Total memory available (including buffered and cached): %ld bytes\n", total_memory);
return 0;
}
sysinfo structAnother way to get some memory stats is by using the sysinfo function which provides system-wide statistics including total RAM, free RAM, and used RAM. However, individual metrics for cached and buffered RAM aren’t directly provided.
Here's how you might use sysinfo:
#include <stdio.h>
#include <sys/sysinfo.h>
int main() {
struct sysinfo memInfo;
sysinfo(&memInfo);
long total_ram = memInfo.totalram; // in bytes
long total_cached = memInfo.totalswap; // approximate as it provides total swap (could vary)
printf("Total RAM: %ld bytes\n", total_ram);
printf("Approx. Total Cached RAM: %ld bytes (use from proc for exact)\n", total_cached);
return 0;
}
/proc/meminfo is the most complete and gives you the most relevant information including total, buffered, and cached RAM.fopen, fgets, and sscanf for robustness./proc/meminfo are typically in kilobytes.You can combine these methods to get a better overview of the memory within a Linux environment, depending on your application's needs.
Not yet included in a published release.