File tree Expand file tree Collapse file tree 6 files changed +40
-5
lines changed
common/management/commands/services/services Expand file tree Collapse file tree 6 files changed +40
-5
lines changed Original file line number Diff line number Diff line change @@ -19,7 +19,6 @@ def cmd(self):
1919 cmd = [
2020 'gunicorn' , 'maxkb.wsgi:application' ,
2121 '-b' , bind ,
22- '--preload' ,
2322 '-k' , 'gthread' ,
2423 '--threads' , '200' ,
2524 '-w' , str (self .worker ),
Original file line number Diff line number Diff line change @@ -29,7 +29,6 @@ def cmd(self):
2929 cmd = [
3030 'gunicorn' , 'maxkb.wsgi:application' ,
3131 '-b' , bind ,
32- '--preload' ,
3332 '-k' , 'gthread' ,
3433 '--threads' , '200' ,
3534 '-w' , str (worker ),
Original file line number Diff line number Diff line change @@ -21,7 +21,6 @@ def cmd(self):
2121 cmd = [
2222 'gunicorn' , 'maxkb.wsgi:application' ,
2323 '-b' , bind ,
24- '--preload' ,
2524 '-k' , 'gthread' ,
2625 '--threads' , '200' ,
2726 '-w' , str (self .worker ),
Original file line number Diff line number Diff line change 99from .base import *
1010from .logging import *
1111from .auth import *
12- from .lib import *
12+ from .lib import *
13+ from .mem import *
Original file line number Diff line number Diff line change 1+ # coding=utf-8
2+ import os
3+ import gc
4+ import threading
5+ from maxkb .const import CONFIG
6+ from common .utils .logger import maxkb_logger
7+ import tracemalloc
8+
9+ CURRENT_PID = os .getpid ()
10+ GC_THRESHOLD = (100 , 5 , 5 )
11+ # 1 hour
12+ GC_INTERVAL = 3600
13+
14+ def change_gc_threshold ():
15+ old_threshold = gc .get_threshold ()
16+ gc .set_threshold (* GC_THRESHOLD )
17+ maxkb_logger .debug (f"(PID: { CURRENT_PID } ) GC thresholds changed from { old_threshold } → { GC_THRESHOLD } " )
18+
19+
20+ def force_gc ():
21+ snapshot = tracemalloc .take_snapshot ()
22+ top_stats = snapshot .statistics ('lineno' )
23+ maxkb_logger .debug ("[ Top 10 memory-consuming lines ]" )
24+ for stat in top_stats [:10 ]:
25+ maxkb_logger .debug (stat )
26+ collected = gc .collect ()
27+ maxkb_logger .debug (f"(PID: { CURRENT_PID } ) Forced GC ({ collected } objects collected)" )
28+ threading .Timer (GC_INTERVAL , force_gc ).start ()
29+
30+
31+ def init_memory_optimization ():
32+ tracemalloc .start ()
33+ change_gc_threshold ()
34+ force_gc ()
35+ maxkb_logger .debug ("(PID: {CURRENT_PID}) Memory optimization (GC tuning) started." )
36+
37+ if CONFIG .get ("ENABLE_MEMORY_OPTIMIZATION" , '1' ) == "1" :
38+ init_memory_optimization ()
Original file line number Diff line number Diff line change @@ -31,7 +31,6 @@ RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
3131 chmod g+xr /usr/bin/ld.so && \
3232 chmod g+x /usr/local/bin/python* && \
3333 apt-get clean all && \
34- echo "/usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2" > /etc/ld.so.preload && \
3534 rm -rf /var/lib/apt/lists/* /usr/share/doc/* /usr/share/man/* /usr/share/info/* /usr/share/locale/* /usr/share/lintian/* /usr/share/linda/* /var/cache/* /var/log/* /var/tmp/* /tmp/*
3635COPY --from=vector-model --chmod=700 /opt/maxkb-app/model /opt/maxkb-app/model
3736
You can’t perform that action at this time.
0 commit comments