Skip to main content

Function Signature

InformationMemory() -> str | None

Overview

The InformationMemory() function reads system memory information from /proc/meminfo and returns a formatted string containing key memory statistics including total memory, free memory, buffers, cached memory, and calculated used memory. This function parses the /proc/meminfo file to extract essential memory metrics and presents them in a human-readable format with values in kilobytes (kB).

Return Value

return
str | None
Returns a formatted string containing memory statistics, or None if the memory information cannot be read.The string format is:
Memoria Total: {mem_total} kB
Memoria Libre: {mem_free} kB
Buffers: {buffers} kB
Cached: {cached} kB
Memoria Usada: {mem_used} kB

Implementation Details

The function performs the following operations:
  1. Reads /proc/meminfo: Uses pathlib.Path to read the contents of the system memory information file
  2. Parses memory values: Extracts four key metrics:
    • MemTotal: Total usable RAM (physical RAM minus reserved bits and kernel binary code)
    • MemFree: Sum of LowFree + HighFree
    • Buffers: Relatively temporary storage for raw disk blocks
    • Cached: In-memory cache for files read from disk
  3. Calculates used memory: Computes actual used memory as:
    mem_used = mem_total - mem_free - buffers - cached
    
  4. Returns formatted output: All values are returned in kilobytes (kB)
The function returns None if the /proc/meminfo file cannot be read or is empty. This typically only occurs on non-Linux systems or in environments where /proc is not mounted.

Example Output

Memoria Total: 16384000 kB
Memoria Libre: 8192000 kB
Buffers: 512000 kB
Cached: 2048000 kB
Memoria Usada: 5632000 kB

Usage Example

from memory_info import InformationMemory

# Get memory statistics
mem_stats = InformationMemory()

if mem_stats:
    print(mem_stats)
else:
    print("Unable to retrieve memory information")

Source Code Reference

The implementation can be found in memory_info.py:4-32.

See Also

Build docs developers (and LLMs) love