Skip to content

Instantly share code, notes, and snippets.

@martinth
Created August 17, 2015 10:20
Show Gist options
  • Save martinth/1042c452046600529e52 to your computer and use it in GitHub Desktop.
Save martinth/1042c452046600529e52 to your computer and use it in GitHub Desktop.
from pympler import summary, muppy
import psutil
# source: http://www.mobify.com/blog/sqlalchemy-memory-magic/
def get_virtual_memory_usage_kb(): # pragma: no cover
"""
The process's current virtual memory size in Kb, as a float.
"""
return float(psutil.Process().memory_info_ex().vms) / 1024.0
def memory_usage(where, object_limit=2): # pragma: no cover
"""
Print out a basic summary of memory usage.
"""
mem_summary = summary.summarize(muppy.get_objects())
print("Memory summary:", where)
summary.print_(mem_summary, limit=object_limit)
print("VM: %.2fMb" % (get_virtual_memory_usage_kb() / 1024.0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment