0

I am trying check which part costs the most memory for my python program. Using this post, http://practicalpython.blogspot.com/2017/03/monitoring-memory-usage-in-jupyter.html.

The code is:

import numpy as np
import pandas as pd
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))

However, I got the error

  File "<ipython-input-437-c6c16f08fe15>", line 1
    for k,(value,*names) in vars.items():
                 ^
SyntaxError: invalid syntax

Anyone knows what's happening? Thanks.

nj2237
  • 1,220
  • 3
  • 21
  • 25
user40780
  • 1,828
  • 7
  • 29
  • 50

0 Answers0