segfault
segfault

Reputation: 5939

How to get buffered/cached memory size in C++ under Linux?

I want to warn the user when memory available is low. Currently I'm using sysconf(_SC_PHYS_PAGES) to get the number of physical pages available.

However, there is also memory that the OS uses as buffer and cache. How do I obtain them programmatically?

Upvotes: 2

Views: 2784

Answers (1)

CrazyCasta
CrazyCasta

Reputation: 28302

The way the free command from procps does it is by reading the /proc/meminfo file. You can see their source here. The meminfo function updates globals, in particular kb_main_buffers and kb_main_cached. You could probably reuse their code to do what you want. (Assuming your license is compatible)

Upvotes: 4

Related Questions