Python程序内存占用计算:三种实现方法差异解析及实现示例咨询
Hey there! I've dug into Python memory profiling tools quite a bit, and the big discrepancies you're seeing make total sense—these three methods measure completely different layers of memory usage. Let's break down each one, compare them, and figure out which fits your LeetCode-style memory tracking goal best.
1. resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
This taps into your OS's built-in resource tracking system. It measures the peak Resident Set Size (RSS) of your entire Python process during its lifetime. RSS is the actual physical memory your process is using at any given moment, and ru_maxrss captures the highest value of that during execution.
- Pros: Gets a system-level peak memory reading, which aligns exactly with what LeetCode shows for submitted code.
- Cons: Platform-dependent units (KB on Linux, bytes on macOS, not supported on Windows), and only gives you the peak—no real-time tracking or granular details.
2. psutil.Process(os.getpid())
Psutil is a cross-platform system monitoring library that gives you deep, real-time access to your process's stats. The line you wrote just creates a process object; to get memory data, you'll need to call methods like .memory_info() (for RSS/virtual memory) or .memory_full_info() (for more granular stats like shared memory, swap usage).
- Pros: Cross-platform compatible, lets you track real-time memory usage and capture peaks, plus it offers tons of extra system metrics if you need them later.
- Cons: Requires installing a third-party package (
pip install psutil), but that's a tiny tradeoff for flexibility.
3. tracemalloc.get_traced_memory()
Tracemalloc is Python's built-in tool for tracking memory allocated by Python code specifically. It only counts memory used by Python objects (lists, dicts, etc.) managed by the interpreter—it ignores memory used by the Python interpreter itself, C extensions, or system-level resources.
- Pros: Great for debugging Python-specific memory leaks, can even show you which lines of code are allocating the most memory.
- Cons: Way too narrow for your use case—LeetCode shows system-level memory usage, not just Python object memory.
LeetCode displays the system-level peak physical memory your code uses during execution. So:
- If you only need a quick peak reading and don't care about Windows support,
resource.getrusageworks. - For a robust, cross-platform solution that lets you track real-time usage and capture peaks, psutil is the best choice—it's flexible, reliable, and aligns perfectly with what LeetCode shows.
- Tracemalloc is better for Python-specific memory debugging, not system-level stats.
Here's a practical example using psutil to track peak memory usage, just like LeetCode does:
import os import psutil import time import threading def track_peak_memory(pid, check_interval=0.01): """Background thread to monitor and record peak memory usage""" process = psutil.Process(pid) peak_rss_mb = 0 while True: try: # Get current physical memory usage (RSS) and convert to MB current_rss = process.memory_info().rss / (1024 * 1024) if current_rss > peak_rss_mb: peak_rss_mb = current_rss time.sleep(check_interval) except psutil.NoSuchProcess: # Exit when the target process finishes break print(f"✅ Peak Memory Used: {peak_rss_mb:.2f} MB") def your_test_function(): """Replace this with the function you want to test""" # Example: Create a large list to consume memory large_dataset = [i for i in range(10**7)] time.sleep(0.5) # Simulate work being done return large_dataset if __name__ == "__main__": # Get the ID of the current Python process current_pid = os.getpid() # Start the memory monitoring thread monitor_thread = threading.Thread(target=track_peak_memory, args=(current_pid,)) monitor_thread.start() # Run your target function result = your_test_function() # Wait for the monitor thread to finish and print results monitor_thread.join() # Optional: Check memory usage right after the function finishes post_process = psutil.Process(current_pid) post_rss_mb = post_process.memory_info().rss / (1024 * 1024) print(f"📌 Post-Execution Memory: {post_rss_mb:.2f} MB")
If you want a simpler, Linux/macOS-only version using resource.getrusage:
import resource import time def your_test_function(): large_dataset = [i for i in range(10**7)] time.sleep(0.5) return large_dataset if __name__ == "__main__": # Capture initial resource usage start_usage = resource.getrusage(resource.RUSAGE_SELF) # Run the test function your_test_function() # Capture final resource usage (ru_maxrss is the peak RSS) end_usage = resource.getrusage(resource.RUSAGE_SELF) # Convert KB to MB (Linux/macOS specific) peak_memory_mb = end_usage.ru_maxrss / 1024 print(f"✅ Peak Memory Used: {peak_memory_mb:.2f} MB")
Just remember: Windows doesn't support the resource module, so use psutil if you need cross-platform compatibility.
内容的提问来源于stack exchange,提问作者dumbQuestions

