Complete the code to import the module used for memory profiling.
import [1]
The memory_profiler module is used to analyze memory usage in Python.
Complete the code to get the current memory usage of the Python process in MB.
import psutil process = psutil.Process() mem = process.[1]().rss / (1024 * 1024) print(f"Memory usage: {mem:.2f} MB")
The memory_info() method returns memory usage details of the process.
Fix the error in the code to correctly measure memory usage of a function using a decorator.
@memory_profiler.[1] def my_function(): a = [i for i in range(100000)] return sum(a)
The @memory_profiler.profile decorator measures memory usage of the decorated function.
Fill both blanks to create a dictionary comprehension that maps variable names to their memory size in bytes, filtering only variables larger than 1000 bytes.
memory_usage = {var: sys.getsizeof([1]) for var in globals() if sys.getsizeof([2]) > 1000}We use globals()[var] to get the value of each variable by name for size checking.
Fill all three blanks to create a list comprehension that collects sizes of all list variables in locals() larger than 5000 bytes.
sizes = [sys.getsizeof([1]) for [2], [3] in locals().items() if isinstance([1], list) and sys.getsizeof([1]) > 5000]
We iterate over locals().items() with name, value. The variable value is used to check type and size.