Skip to content

Memory leak in serverless / container

Yandex Cloud Functions have a memory limit; a 100 MB skew is enough to get an OutOfMemory under load. A simple watchdog:

import os, json, time, resource, requests
S = "/tmp/mem-trend.json"
def handler(event, context):
rss_mb = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024
s = (json.load(open(S)) if os.path.exists(S) else {"hist": []})
s["hist"] = (s["hist"] + [rss_mb])[-30:]
json.dump(s, open(S, "w"))
if len(s["hist"]) == 30:
first, last = s["hist"][0], s["hist"][-1]
if last > first * 1.5 and last > 200:
push("🧠 Memory leak подозрение",
f"RSS: {first:.0f}{last:.0f} MB за 30 запусков",
priority=8)
return {"statusCode": 200}
def push(t, m, p):
requests.post(f"{os.environ['NOTIFLY_URL']}/message",
params={"token": os.environ["NOTIFLY_TOKEN"]},
json={"title": t, "message": m, "priority": p}, timeout=5)

For Docker/k8s — the same using node_exporter / docker stats.