Explain Codes LogoExplain Codes Logo

Total memory used by Python process?

python
memory-management
performance-optimization
python-stdlib
Anton ShumikhinbyAnton Shumikhin·Oct 21, 2024
TLDR

Get a snapshot of memory footprint with psutil:

import psutil print(f"Memory: {psutil.Process().memory_info().rss / 1024 ** 2:.2f} MB")

This command will report the Resident Set Size (RSS), the actual RAM usage, in MB for the current process.

Delving into memory management

Understanding the fine intricacies of memory usage in Python is important for optimizing the performance of your applications. The psutil library is a key resource, giving an ocean of data about system and process information.

  1. Installation

    You might need pip install psutil if it's absent on your system.

  2. Memory in bytes

    To fetch the current memory usage in bytes, use:

    import psutil process = psutil.Process() print(f"# I'm byteful, aren't I? 🤓 : {process.memory_info().rss} bytes")
  3. Python 2.7 or psutil 5.6.3

    If you're on Python 2.7 or psutil version 5.6.3, please consider:

    print(process.memory_info()[0]) # Old but gold!
  4. Windows System

    On Windows, you need to get acquainted with Windows Management Instrumentation (WMI). You can fetch memory usages via WorkingSet on WMI using the WMI library.

  5. Memory quest on Linux

    If you're a Linux user, you can bravely journey through /proc/self/status or /proc/self/statm for memory metrics. Mind you, these are measured in kilobytes (kB), so make sure to convert it:

    memory_in_kb = float(process.memory_info().rss) / 1024 memory_in_mb = memory_in_kb / 1024 print(f"Who likes memory in MB? {memory_in_mb:.2f} MB")
  6. UNIX Commander

    If you're sophisticated enough to use the UNIX command ps -u -p <process_id> for a specific process ID, hats off to you! It's a handy way to monitor memory usage.

Cross-platform memory reconnaissance

All environments are not created equal. Some methods to retrieve memory metrics are platform-dependent. For instance, using getrusage(resource.RUSAGE_SELF) on Unix-like systems will provide data for the calling process, and getrusage(resource.RUSAGE_BOTH) where available, can be used to extract memory usage of both the children and parent processes.

Linux Fun Fact: Parsing /proc/self/status can give you the good old VmRSS (Resident Set Size) directly. If you're living large, you can convert memory values using a scale dictionary:

scales = {'Joules': 1, 'calories': 4.18, 'kWh': 3.6e6} # Always too much energy! memory_usage = memory_rss * scales['calories'] # Convert Joules to calories

And remember, never forget to quip because "An apple a day keeps the docstring away!"

Best practice: Granular control of memory management

For the control freaks among us who need fine-grained, line-by-line memory management, look no further than memory_profiler, a nifty module that's a pip install away. Your granular view of memory consumption during runtime awaits.

Ever dreamed of becoming a detective? Try out the tracemalloc module, a Sherlock Holmes for tracking memory allocations in Python. Crack open the mysteries of memory leaks and allocation patterns as you deep dive into Python memory management.

Proactive checks and trade-offs

"Don't just solve problems, prevent them!", that's the mantra in professional development. Early identification of memory-intensive parts during development can save you from that future "Oh, Snap!" moment. Flex your unit test muscles with memory usage checks and stop memory regressions in their tracks.

Different data structures can lend different efficiencies. For instance, __slots__ in classes can be the memory-saving hero when creating a multitude of objects. Sadly, poor __slots__ never gets the girl in the end. 🦸💔