Memory usage warning.

Why is this not a thing. This is simple. Why has nobody made this yet. Why is there not a script circulating on the internet. Why. Why. Why are there so many programs to monitor memory usage but none to specifically alert you in case the thing clogging your memory is an hour of unsaved work. Why. What idiot came up with this system.

If you've ever had to search for this and come up empty-handed, what search terms did you use? Let's prove that SEO is not merely a tool of the Devil.

(If you are not code-savvy, this is Python. This script is also specialized for Linux. If you are on Linux and not code-savvy, you need more help than either I or this script can give you. Godspeed.)
import re
import time
import os

extractNumber = re.compile("\d+")

while True:
    time.sleep(10)
    f = open("/proc/meminfo")
    data = f.read()
    f.close()
    allMem = 0
    avlMem = 0
    for line in data.split("\n"):
        if line.startswith("MemTotal:"):
            allMem = extractNumber.search(line).group(0)
        if line.startswith("MemAvailable"):
            avlMem = extractNumber.search(line).group(0)
        if allMem and avlMem:
            break
    pctAvailable = float(avlMem) / float(allMem)
    if pctAvailable < 0.1:
        pct = round(pctAvailable * 100, 1)
        os.system("notify-send 'Memory Usage Alert!' '" + str(pct) + "% available'")

Comments