title: Jupyter notebook: memory usage for each notebook
body: <p>The memory on my lab's server (Ubuntu) is constantly filling up due to users never shutting down old notebooks. I would like to get a better idea of how much memory each notebook is taking up. I can summarize (rough) memory usage for all jupyter notebooks run by each user, but I would like to get the total memory usage of each individual notebook so that I can shut down those particular memory hogs (or tell another user to shut his/her's down). I quickly put together the following code to get approx. mem. usage per jupyter kernel, but I don't know how to associate the kernel IDs to a particular notebook. </p>
  
  <pre><code>import os
  import pwd
  import pandas as pd
  
  UID   = 1
  EUID  = 2
  
  pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  
  df = []
  for pid in pids:
      try:
          ret = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read()
      except IOError: # proc has already terminated
          continue
  
      # jupyter notebook processes
      if len(ret) &gt; 0 and 'share/jupyter/runtime' in ret:
          process = psutil.Process(int(pid))
          mem = process.memory_info()[0] 
  
          # user name for pid
          for ln in open('/proc/%d/status' % int(pid)):
              if ln.startswith('Uid:'):
                  uid = int(ln.split()[UID])
                  uname = pwd.getpwuid(uid).pw_name
  
          # user, pid, memory, proc_desc
          df.append([uname, pid, mem, ret])
  
  df = pd.DataFrame(df)
  df.columns = ['user', 'pid', 'memory', 'proc_desc']
  df
  </code></pre>