{"id":1843,"library":"memory-profiler","title":"Memory Profiler","description":"memory-profiler is a Python module designed for monitoring the memory consumption of a Python process, including detailed line-by-line analysis of memory usage within Python programs. It is built purely in Python and depends on the `psutil` module. The current version is 0.61.0. As of the latest information, the package is no longer actively maintained by its original developers.","status":"maintenance","version":"0.61.0","language":"en","source_language":"en","source_url":"https://github.com/pythonprofilers/memory_profiler","tags":["memory","profiling","performance","debugging","resource-management"],"install":[{"cmd":"pip install -U memory-profiler","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for process memory monitoring capabilities.","package":"psutil","optional":false}],"imports":[{"note":"When using `mprof run` to collect time-based memory usage with timestamps, do NOT import `profile` within your script. Only apply the `@profile` decorator to functions you want to profile. Importing `profile` will prevent timestamps from being recorded by `mprof`.","wrong":"from memory_profiler import profile # when running with mprof run","symbol":"profile","correct":"from memory_profiler import profile"},{"symbol":"memory_usage","correct":"from memory_profiler import memory_usage"}],"quickstart":{"code":"import time\nfrom memory_profiler import profile\n\n# Save this as 'my_script.py'\n\n@profile\ndef create_large_lists():\n    a = [0] * (10 ** 6)  # Allocates ~8MB\n    time.sleep(0.1)\n    b = [1] * (2 * 10 ** 7) # Allocates ~160MB\n    time.sleep(0.1)\n    c = [2] * (5 * 10 ** 6)  # Allocates ~40MB\n    del b # Frees ~160MB\n    return a, c\n\nif __name__ == '__main__':\n    print(\"Starting memory intensive task...\")\n    lists = create_large_lists()\n    print(\"Task completed.\")\n    # The profiling output will be printed to stdout when run via python -m memory_profiler","lang":"python","description":"To perform a line-by-line memory usage analysis, decorate the function you want to profile with `@profile`. Then, run your script using the `python -m memory_profiler` command. The output will be printed to standard output, showing memory usage and increments for each line within the decorated function."},"warnings":[{"fix":"For new projects, consider exploring more actively maintained memory profiling alternatives (e.g., `memray`, `fil-profiler`). For existing projects, be aware that support might be limited.","message":"The `memory-profiler` package is no longer actively maintained by its original developers. While functional, new features or active issue resolution are unlikely.","severity":"deprecated","affected_versions":"0.61.0 onwards"},{"fix":"Comment out the `from memory_profiler import profile` statement when running with `mprof run` to ensure timestamps are captured, leaving only the `@profile` decorator on functions.","message":"When using the `mprof run` command-line utility for time-based memory usage with timestamps, importing `@profile` directly in your Python script (`from memory_profiler import profile`) will prevent timestamps from being recorded.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `memory-profiler` exclusively for development and debugging. For production memory monitoring, consider less intrusive system-level tools or Python's built-in `tracemalloc` for lower overhead.","message":"Using `memory-profiler` introduces overhead and consumes additional memory itself. It is not recommended for production environments due to its intrusive nature.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use the `mprof run --include-children` or `--multiprocess` flags to track child processes. When using the `memory_usage` API directly, the return value will include child memory in a nested list that needs to be manually processed.","message":"By default, `memory-profiler` only tracks the memory usage of the parent process. Memory consumed by forked child processes (e.g., when using `multiprocessing`) is not automatically included in the report.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If encountering unusual behavior when profiling, test the function without the `@profile` decorator. If the issue resolves, consider using the `memory_usage` function for profiling specific code blocks, or explore non-intrusive profilers like `fil-profiler`.","message":"Applying the `@profile` decorator can sometimes subtly change the behavior of the decorated function, particularly when interacting with complex data structures or libraries like Pandas, potentially leading to unexpected errors or different outputs than when run un-profiled.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}