u/JellyGrimm

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.

Web: https://www.willmanstoolbox.com/libtrm/

Repo: https://github.com/willmanstoolbox/libtrm

reddit.com
u/JellyGrimm — 11 hours ago

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.

Web: https://www.willmanstoolbox.com/libtrm/

Repo: https://github.com/willmanstoolbox/libtrm

reddit.com
u/JellyGrimm — 11 hours ago
▲ 28 r/software+1 crossposts

A tiny C utility to send files to your phone via QR

I move files between my PC and mobile quite often. Tools like KDE Connect feel like overkill for simple transfers, and setting up a temporary http server every time is tedious because it still requires manually typing IPs and ports on the phone.

So I made a basic utility that spawns a temporary local server and generates a QR code. You scan the code with your phone and download the file(s) directly over your local network.

I wrote it in pure C using Nuklear for the GUI. The goal was to keep it as lightweight as possible; the Linux builds are around 230 KB. On Windows, it integrates into the right-click context menu, and on Linux, it works with "Open With" menu, or in any case you can just open the program and drag and drop any files you want. It doesn't use the cloud or any external servers, it all happens in your cpu.

I'm pretty happy with how lightweight it turned out. I plan on adding bidirectional support later and make a separate binary that only contains the underlying CLI (some people may want to use it in servers for example) and actually make a decent UI, but for now, it does exactly what it says and it does it well. If anyone else finds it useful or has technical feedback, it’s appreciated.

Web: https://www.willmanstoolbox.com/phonedrop/

Repo: https://github.com/willmanstoolbox/phonedrop

reddit.com
u/JellyGrimm — 2 days ago