Overview
Memory Monitor tracks system memory usage by reading data directly from the/proc/meminfo file, a virtual file provided by the Linux kernel that contains real-time memory statistics.
How /proc/meminfo Works
The/proc/meminfo file is a virtual file that doesn’t actually exist on disk. Instead, the Linux kernel generates its contents dynamically whenever a process reads it. This file provides a comprehensive snapshot of the system’s current memory state.
The
/proc filesystem is a pseudo-filesystem that provides an interface to kernel data structures. It’s called “proc” because it was originally designed to provide information about processes.Memory Metrics Explained
Memory Monitor tracks five key memory metrics:MemTotal
MemTotal
The total amount of usable RAM in the system (physical RAM minus reserved bits and kernel binary code). This value remains constant unless hardware changes occur.
MemFree
MemFree
The amount of physical RAM that is completely unused by the system. This does NOT represent available memory, as it doesn’t include memory used for buffers and cache that can be reclaimed.
Buffers
Buffers
Memory used for temporary storage of raw disk blocks. This is relatively small and shouldn’t get very large.
Cached
Cached
Memory used to cache files read from disk (page cache). The kernel can reclaim this memory when applications need it, making it effectively available.
MemUsed (Calculated)
MemUsed (Calculated)
The actual memory being used by applications and the kernel. This is calculated as:This formula gives you the memory that is actively in use and cannot be easily reclaimed.
Implementation Details
Reading Memory Information
TheInformationMemory() function reads and parses /proc/meminfo using Python’s pathlib module:
memory_info.py
The function uses
pathlib.Path for a modern, object-oriented approach to file handling. The read_text() method reads the entire file contents as a string.Parsing Strategy
The parser iterates through each line of/proc/meminfo and extracts only the metrics it needs:
memory_info.py
- Splits the text into lines using
splitlines() - Filters for relevant metrics using
startswith()checks - Extracts values by splitting on whitespace and taking the second element (index 1)
- Converts to integers for mathematical operations
Calculating Memory Usage
The actual memory in use is calculated by subtracting reclaimable memory from the total:memory_info.py
Output Format
The function returns a formatted string with all memory metrics:memory_info.py
Key Design Decisions
Why not use psutil or other libraries?
Why not use psutil or other libraries?
Memory Monitor is designed to be lightweight with minimal dependencies. Reading from
/proc directly eliminates external dependencies and provides full control over what data is collected.Why filter only specific metrics?
Why filter only specific metrics?
/proc/meminfo contains dozens of memory metrics. By filtering for only the essential metrics, Memory Monitor minimizes processing time and keeps the output focused on the most relevant information.Why calculate MemUsed instead of reading it?
Why calculate MemUsed instead of reading it?
/proc/meminfo doesn’t provide a “MemUsed” field directly. The calculation MemTotal - MemFree - Buffers - Cached is the standard way to determine actual memory usage on Linux systems.Example /proc/meminfo Output
Here’s what a typical/proc/meminfo file looks like:
Next Steps
Process Analysis
Learn how Memory Monitor identifies which processes are consuming the most memory