A tiny, single-header C library to track true RAM usage on Linux
Working in C lately made me realize there is no drag and drop way to measure true ram usage, because when you ask the OS it will give you whatever your program is using PLUS the shared libraries, so if your code is actually being executed in a few kb of memory it may seem like it's megabytes simply because there is no clean way to ask for the true RAM usage. I looked for a drag and drop library where I could just drop an .h file into my project and get the proportional set size and be able to monitor this, but I could not find anything lightweight and dependency-free. So I wrote this library, which is literally a library for true ram usage, hence the libtrm name.
The way this works is, I just made an ASCII parser to rip the data directly from the /proc files in the kernel. It tries to use the modern smaps_rollup fast path but automatically falls back to parsing the full smaps for older Linux kernels from before 2017, in case someone still uses that. You can then use really simple calls to that data to log them at any point in your program. I used kilobytes and bytes since, you know, this is C. You can also diff how much RAM usage the OS was reporting against what you truly used.
I also included a main.c that acts as an interactive tutorial. It runs a stress test shows how PSS barely moves when you malloc(), but spikes the second you actually memset() data into it. I encourage you to tinker with it, it makes it easier to understand the commands.
I am happy with how lean it turned out. It is perfect for developers who want to add a live RAM display to their tools without adding overhead. Feedback on the parser logic is appreciated.