Skip to content

Instantly share code, notes, and snippets.

@nnathan
Last active April 20, 2022 02:17
Show Gist options
  • Save nnathan/c97cf903ca2bdf8a5f84 to your computer and use it in GitHub Desktop.
Save nnathan/c97cf903ca2bdf8a5f84 to your computer and use it in GitHub Desktop.
Example of logging memory usage before and after invoking a function in Python using decorators
# Running the script below will yield the following results:
#
# $ ipython log_memory_usage_example.py
# INFO:root:memory use before calling make_large_string : 19,340 kb
# INFO:root:memory use after calling make_large_string : 85,840 kb
#
import functools
import logging
import resource
def log_memory_usage(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
logging.info(
'memory use before calling {} : {:,} kb'.format(
func.__name__,
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
)
)
return_value = func(*args, **kwargs)
logging.info(
'memory use after calling {} : {:,} kb'.format(
func.__name__,
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
)
)
return return_value
return wrapper
# keep a reference to prevent garbage collection
# when calling make_large_string()
large_string = None
@log_memory_usage
def make_large_string():
global large_string
# use >= 64MB of memory
large_string = "a"*1024*1024*64
logging.basicConfig(level=logging.DEBUG)
make_large_string()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment