Skip to content

Commit 1c75e2d

Browse files
committed
Remove custom free memory-calculation
The kernel exposes a field specifically for this, which makes the calculation independt of kernel-specifics, that might change in the future. Also fix a memory leak.
1 parent 2612ff2 commit 1c75e2d

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

sysstat.c

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ get_user_home(void)
257257
static char *
258258
get_ram_usage(void)
259259
{
260-
unsigned long long reclaimable, total, free, buffers, cached, shmem;
261-
unsigned long long usage = 0;
260+
unsigned long long total = 0, available = 0, usage = 0;
262261
FILE *meminfo;
263262
char buf[256];
264263

@@ -272,27 +271,15 @@ get_ram_usage(void)
272271
break;
273272
}
274273

275-
if (strncmp(buf, "SReclaimable:", 13) == 0) {
276-
sscanf(buf, "%*s %llu", &reclaimable);
277-
}
278-
else if (strncmp(buf, "MemTotal:", 9) == 0) {
274+
if (strncmp(buf, "MemTotal:", 9) == 0) {
279275
sscanf(buf, "%*s %llu", &total);
280276
}
281-
else if (strncmp(buf, "MemFree:", 8) == 0) {
282-
sscanf(buf, "%*s %llu", &free);
283-
}
284-
else if (strncmp(buf, "Buffers:", 8) == 0) {
285-
sscanf(buf, "%*s %llu", &buffers);
286-
}
287-
else if (strncmp(buf, "Cached:", 7) == 0) {
288-
sscanf(buf, "%*s %llu", &cached);
289-
}
290-
else if (strncmp(buf, "Shmem:", 6) == 0) {
291-
sscanf(buf, "%*s %llu", &shmem);
277+
else if (strncmp(buf, "MemAvailable:", 13) == 0) {
278+
sscanf(buf, "%*s %llu", &available);
292279
}
293280
}
294281

295-
usage = total - free - cached + shmem - buffers - reclaimable;
282+
usage = total - available;
296283
/* Values in /proc/meminfo are in Kibibyte */
297284
usage *= 1024;
298285

@@ -620,10 +607,11 @@ print_status(void)
620607
fwrite(yajl_output, 1, yajl_output_len, stdout);
621608
yajl_gen_clear(yajl_generator);
622609

623-
putchar('\n');
610+
printf("\n");
624611
fflush(stdout);
625612

626613
free(datetime);
614+
free(datetime_fuzzy);
627615
free(uptime);
628616
free(ram);
629617
free(storage);

0 commit comments

Comments
 (0)