Python Detect Memory Usage of Current Program

October 8, 2019
pip install psutil
import psutil
import logging

log = ...

def print_memory_usage(message):
    # return the memory usage in percentage like top
    process = psutil.Process(os.getpid())
    # mem = process.memory_percent()
    memory = int(process.memory_info().rss / 1_000_000)
    log.info(f"{message}, Memory={memory}MB")

print_memory_usage('start')
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.