Files

46 lines
1.2 KiB
Python

import os, time
STEP = 256 * 1024 * 1024
MAX = 3584 * 1024 * 1024
MIN_AVAILABLE_KB = 700 * 1024
HOLD_SECONDS = 12
PAGE = 4096
try:
with open("/proc/self/oom_score_adj", "w") as f:
f.write("1000")
except Exception as e:
print("oom_score_adj_error", repr(e), flush=True)
def meminfo():
out = {}
with open("/proc/meminfo") as f:
for line in f:
key, rest = line.split(":", 1)
if key in ("MemAvailable", "SwapFree", "SwapTotal"):
out[key] = int(rest.strip().split()[0])
return out
chunks = []
allocated = 0
print("start", meminfo(), flush=True)
while allocated < MAX:
current = meminfo()
if current["MemAvailable"] < MIN_AVAILABLE_KB:
print("stop_before_step", "allocated_bytes", allocated, current, flush=True)
break
b = bytearray(STEP)
for i in range(0, len(b), PAGE):
b[i] = 1
chunks.append(b)
allocated += STEP
print("step", "allocated_bytes", allocated, meminfo(), flush=True)
print("hold", HOLD_SECONDS, "allocated_bytes", allocated, meminfo(), flush=True)
time.sleep(HOLD_SECONDS)
print("release", "allocated_bytes", allocated, meminfo(), flush=True)
chunks.clear()
time.sleep(2)
print("after_release", meminfo(), flush=True)