Skip to content

Instantly share code, notes, and snippets.

@georgepsarakis
Created August 13, 2013 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgepsarakis/6219940 to your computer and use it in GitHub Desktop.
Save georgepsarakis/6219940 to your computer and use it in GitHub Desktop.
Function to return memory consumption for the script itself.
from resource import getrusage, RUSAGE_SELF
def monitor(unit="M", alert=None):
unit = unit.upper()
unit_transformation = {
"B" : 1./1024.,
"K" : 1.,
"M" : 1024.,
"G" : 1024.*1024.,
}
if not unit in unit_transformation.keys():
unit = "K"
mem = getrusage(RUSAGE_SELF).ru_maxrss
mem /= unit_transformation[unit]
if not alert is None:
status = mem > alert
else:
status = None
return (status, mem,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment