0
0
Data Analysis Pythondata~10 mins

Memory usage analysis in Data Analysis Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the module used for memory profiling.

Data Analysis Python
import [1]
Drag options to blanks, or click blank then click option'
Apandas
Bsys
Cos
Dmemory_profiler
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated modules like os or pandas.
Using sys which is for system-specific parameters, not memory profiling.
2fill in blank
medium

Complete the code to get the current memory usage of the Python process in MB.

Data Analysis Python
import psutil
process = psutil.Process()
mem = process.[1]().rss / (1024 * 1024)
print(f"Memory usage: {mem:.2f} MB")
Drag options to blanks, or click blank then click option'
Amemory_percent
Bmemory_full_info
Cmemory_info
Dmemory_maps
Attempts:
3 left
💡 Hint
Common Mistakes
Using memory_percent which returns a percentage, not bytes.
Using memory_maps which returns memory mapped files info.
3fill in blank
hard

Fix the error in the code to correctly measure memory usage of a function using a decorator.

Data Analysis Python
@memory_profiler.[1]
def my_function():
    a = [i for i in range(100000)]
    return sum(a)
Drag options to blanks, or click blank then click option'
Aprofile
Bprofiled
Cmeasure
Dmemory
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect decorator names like 'profiled' or 'measure'.
Forgetting the decorator symbol '@'.
4fill in blank
hard

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.

Data Analysis Python
memory_usage = {var: sys.getsizeof([1]) for var in globals() if sys.getsizeof([2]) > 1000}
Drag options to blanks, or click blank then click option'
Aglobals()[var]
Blocals()[var]
Cvar
Dglobals()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' directly which is just the variable name string.
Using locals() when variables are in globals().
5fill in blank
hard

Fill all three blanks to create a list comprehension that collects sizes of all list variables in locals() larger than 5000 bytes.

Data Analysis Python
sizes = [sys.getsizeof([1]) for [2], [3] in locals().items() if isinstance([1], list) and sys.getsizeof([1]) > 5000]
Drag options to blanks, or click blank then click option'
Avalue
Bname
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name string instead of the value for size and type checks.
Mixing up the order of key and value in the loop.