Перейти к содержимому

Утечка памяти в serverless / контейнере

Yandex Cloud Functions имеют лимит памяти; перекоса в 100 МБ хватает, чтобы получить OutOfMemory под нагрузкой. Простой 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)

Для Docker/k8s — то же самое из node_exporter / docker stats.