Introduction
The author of this post recently migrated their blog server from Ubuntu to FreeBSD [1]. During this process, they noticed discrepancies in memory usage reporting between fastfetch and btop. This discrepancy led to an in-depth investigation into how modern operating systems, particularly FreeBSD, manage and report memory usage.
Understanding Memory Management
FreeBSD, like other modern operating systems, uses a Virtual Memory (VM) system. This system divides physical memory into pages (usually 4KiB) and manages them through different queues to ensure efficient memory allocation and deallocation [2]. The queues include active, inactive, laundry, wired, and free memory categories, each serving distinct purposes in memory management.
The Role of Disk Cache
The operating system caches disk data in RAM to improve performance. This cache is volatile and can be freed when more memory is needed. The ZFS filesystem, used by FreeBSD, employs an Adaptive Replacement Cache (ARC) to cache recently accessed data [3]. Understanding how this cache operates is crucial for accurately assessing memory usage.
Issues with btop
Investigation revealed that btop had issues with its memory reporting on FreeBSD. Specifically, it used an unsigned 32-bit integer to store memory sizes, which could lead to wrapping issues for systems with more than 4GiB of RAM [4]. Moreover, btop was using a legacy v_cache_count parameter that always returns 0, indicating a need for an alternative approach to track filesystem cache accurately [5].
Crafting a Fix
To address these issues, the author proposed and implemented changes to btop. The fix involved doubling the precision of variables holding queue bytes to prevent wrapping and correctly tracking filesystem cache by utilizing the ARC cache size [6]. A similar fix was also applied to fastfetch [7].
Conclusion
Through this journey, the author gained a deeper understanding of FreeBSD's virtual memory internals and contributed to improvements in btop and fastfetch. This experience highlights the complexity of memory management in modern operating systems and the importance of accurate reporting tools for system administrators [8].
Sources
- https://crocidb.com/post/this-blog-ran-on-ubuntu-16-04-for-10-years-i-migrated-it-to-freebsd/
- https://github.com/freebsd/freebsd-src/blob/584ecfeb9cde1e4cf0ed27c4f822b316b67d320e/sys/vm/vm_page.h#L322C1-L327C19
- https://github.com/htop-dev/htop/pull/2033
- https://github.com/aristocratos/btop/blob/9527231dfbcbe697f8eb80849e267377194e6ca8/src/freebsd/btop_collect.cpp
- https://github.com/aristocratos/btop/pull/1728
- https://github.com/fastfetch-cli/fastfetch/pull/2418
- https://news.ycombinator.com/item?id=48778757
- https://www.reddit.com/r/freebsd/comments/1umc3yc/freebsd_ate_my_ram/

