Распределение памяти Python – разница между psutil и Tracemalloc ⇐ Python
Распределение памяти Python – разница между psutil и Tracemalloc
I am trying to get sense of memory usage of python program. I tried to measure the memory usage using tracemalloc and psutil, however I am getting very different values. What is causing the difference? Which value is "the correct one"? What am I missing, please?
import gc import os import tracemalloc import psutil tracemalloc.start() def get_psutil(): process = psutil.Process(os.getpid()) print(f"[psutil] {process.memory_info().rss / 1024 ** 2}MB") def get_tracemalloc(): current, _ = tracemalloc.get_traced_memory() print(f"[tracemalloc] {current / 10**6}MB") get_psutil() get_tracemalloc() ## large_list = [list(range(1000)) for _ in range(10000)] get_psutil() get_tracemalloc() ## del large_list get_psutil() get_tracemalloc() ## gc.collect() get_psutil() get_tracemalloc() which produces:
[psutil] 10.88671875MB [tracemalloc] 0.000504MB [psutil] 919.92578125MB [tracemalloc] 318.405624MB [psutil] 360.85546875MB [tracemalloc] 0.004592MB [psutil] 353.81640625MB [tracemalloc] 0.001416MB
Источник: https://stackoverflow.com/questions/780 ... racemalloc
I am trying to get sense of memory usage of python program. I tried to measure the memory usage using tracemalloc and psutil, however I am getting very different values. What is causing the difference? Which value is "the correct one"? What am I missing, please?
import gc import os import tracemalloc import psutil tracemalloc.start() def get_psutil(): process = psutil.Process(os.getpid()) print(f"[psutil] {process.memory_info().rss / 1024 ** 2}MB") def get_tracemalloc(): current, _ = tracemalloc.get_traced_memory() print(f"[tracemalloc] {current / 10**6}MB") get_psutil() get_tracemalloc() ## large_list = [list(range(1000)) for _ in range(10000)] get_psutil() get_tracemalloc() ## del large_list get_psutil() get_tracemalloc() ## gc.collect() get_psutil() get_tracemalloc() which produces:
[psutil] 10.88671875MB [tracemalloc] 0.000504MB [psutil] 919.92578125MB [tracemalloc] 318.405624MB [psutil] 360.85546875MB [tracemalloc] 0.004592MB [psutil] 353.81640625MB [tracemalloc] 0.001416MB
Источник: https://stackoverflow.com/questions/780 ... racemalloc
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Получение ошибки «psutil.AccessDenied (pid=6836)» с использованием psutil
Anonymous » » в форуме Python - 0 Ответы
- 28 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Получение ошибки psutil.accessdied (pid = 6836) 'с использованием psutil
Anonymous » » в форуме Python - 0 Ответы
- 18 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Получение ошибки psutil.accessdied (pid = 6836) 'с использованием psutil
Anonymous » » в форуме Python - 0 Ответы
- 5 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Получение ошибки psutil.accessdied (pid = 6836) 'с использованием psutil
Anonymous » » в форуме Python - 0 Ответы
- 7 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Получение ошибки psutil.accessdied (pid = 6836) 'с использованием psutil
Anonymous » » в форуме Python - 0 Ответы
- 14 Просмотры
-
Последнее сообщение Anonymous
-