Sobes.tech
Junior — Middle

How to determine the amount of free RAM on a Linux system?

sobes.tech AI

Answer from AI

In Linux, there are several ways to determine the amount of free RAM. One simple method is to read the /proc/meminfo file, which contains detailed information about memory.

Example in Python:

with open('/proc/meminfo', 'r') as f:
    meminfo = f.read()

# Find the line with MemAvailable — available memory
for line in meminfo.split('\n'):
    if line.startswith('MemAvailable:'):
        available_kb = int(line.split()[1])
        print(f"Available memory: {available_kb / 1024:.2f} MB")
        break

You can also use the free command in the terminal or via subprocess in Python:

import subprocess
result = subprocess.run(['free', '-m'], capture_output=True, text=True)
print(result.stdout)

In the output of free, the fields available or free in the Mem: section are important — they show the amount of free or available memory.

How to determine the amount of free RAM on a Linux… - sobes.tech