my python version is 3.7.9.
from pyinstrument import Profiler
import gc
def a():
profiler = Profiler()
profiler.start()
a=1+1
b=a+2
profiler.stop()
profiler.output_text(unicode=True, color=False, show_all=False, )
profiler.reset()
profiler = None
del profiler
gc.collect()
if __name__ == '__main__':
for i in range(100000000):
a()
pass
When I use pyinstrument, using it in a loop causes memory to continuously increase.
If these two lines ‘del profiler’ and ‘gc.collect()’ are not present, memory will grow very quickly.
How can I completely solve the problem of continuous memory growth?
My actual scenario is to apply it in Flask to count task time, but unfortunately, it also has a memory leak problem.
How to solve it.
@app.before_request
def start_timer():
g.profiler = Profiler()
g.profiler.start()
request.start_time = time.time()
@app.after_request
def log_request(response):
total_time = time.time() - request.start_time
logging.info(f"{total_time :.2f}")
g.profiler.stop()
if total_time > 10:
logging.info(g.profiler.output_text(unicode=True, color=False, show_all=False, ))
g.profiler.reset()
g.profiler = None
return response