Skip to content

Instantly share code, notes, and snippets.

@rtobar
Last active September 26, 2020 02:19
Show Gist options
  • Save rtobar/b1a83a4712f810e6aa227cff988609fc to your computer and use it in GitHub Desktop.
Save rtobar/b1a83a4712f810e6aa227cff988609fc to your computer and use it in GitHub Desktop.
import gc
import os
import resource
import sys
import time
def memusage():
"""memory usage in MB. getrusage defaults to KB on Linux."""
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1e3
def run():
print("Python %s" % sys.version)
print("Memory usage at start: %.2f MB" % memusage())
t_start = time.time()
for i in range(4000000):
pass
print("spent time: {:.2f}".format(time.time() - t_start))
print("Memory usage after loop: %.2f MB" % memusage())
gc.collect()
print("Memory usage after GC: %.2f MB" % memusage())
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment