Last active
March 13, 2020 11:47
-
-
Save sbatururimi/0b64904b1ab575f094996edb96c378fb to your computer and use it in GitHub Desktop.
Checking memory in Jupyter notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def show_mem_usage(): | |
'''Displays memory usage from inspection | |
of global variables in this notebook''' | |
gl = sys._getframe(1).f_globals | |
vars= {} | |
for k,v in list(gl.items()): | |
# for pandas dataframes | |
if hasattr(v, 'memory_usage'): | |
mem = v.memory_usage(deep=True) | |
if not np.isscalar(mem): | |
mem = mem.sum() | |
vars.setdefault(id(v),[mem]).append(k) | |
# work around for a bug | |
elif isinstance(v,pd.Panel): | |
v = v.values | |
vars.setdefault(id(v),[sys.getsizeof(v)]).append(k) | |
total = 0 | |
for k,(value,*names) in vars.items(): | |
if value>1e6: | |
print(names,"%.3fMB"%(value/1e6)) | |
total += value | |
print("%.3fMB"%(total/1e6)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment