Skip to content

Commit 695d59d

Browse files
refactor: trim memory.
1 parent bc1f15e commit 695d59d

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

apps/maxkb/settings/mem.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55
from maxkb.const import CONFIG
66
from common.utils.logger import maxkb_logger
77
import random
8+
import psutil
89

910
CURRENT_PID=os.getpid()
1011
# 1 hour
1112
GC_INTERVAL = 3600
1213

1314
def enable_force_gc():
15+
before = int(psutil.Process(CURRENT_PID).memory_info().rss / 1024 / 1024)
1416
collected = gc.collect()
15-
maxkb_logger.debug(f"(PID: {CURRENT_PID}) Forced GC ({collected} objects collected)")
17+
try:
18+
import ctypes
19+
ctypes.CDLL("libc.so.6").malloc_trim(0)
20+
except Exception:
21+
pass
22+
after = int(psutil.Process(CURRENT_PID).memory_info().rss / 1024 / 1024)
23+
maxkb_logger.debug(f"(PID: {CURRENT_PID}) Forced GC ({collected} objects and {before - after} MB recycled)")
1624
t = threading.Timer(GC_INTERVAL - random.randint(0, 900), enable_force_gc)
1725
t.daemon = True
1826
t.start()

0 commit comments

Comments
 (0)