There are multiple ways to test memory usage in Google Colab. Here are some approaches:
resource
module:import resource
# Get current memory usage
memory_usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print(f"Current memory usage: {memory_usage / 1024:.2f} MB")
This method returns the current memory usage of the Python process in kilobytes. The value can be divided by 1024 to get the usage in megabytes.
psutil
package:!pip install psutil
import psutil
# Get current memory usage
memory_usage = psutil.Process().memory_info().rss
print(f"Current memory usage: {memory_usage / 1024 / 1024:.2f} MB")
This method returns the current memory usage of the Python process in bytes. The value can be divided by 1024 twice to get the usage in megabytes.
memory_profiler
package:!pip install memory_profiler
%load_ext memory_profiler
# Define a function to test memory usage
@profile
def my_function():
# Code to be tested
pass
my_function()
This method requires installing the memory_profiler
package and loading the extension using the %load_ext
magic command. Then, the function to be tested is decorated with the @profile
decorator. When the function is called, the memory usage of each line of code is displayed in the output. This method is useful for identifying memory leaks or optimizing memory-intensive operations.
Asked: 2023-07-11 14:52:51 +0000
Seen: 9 times
Last updated: Jul 11 '23