title: Get python application memory usage
body: <p>My main goal is to know how much memory my python application takes during execution.</p>
  
  <p>I'm using python 2.7.5 on Windows-32 and Windows-64.</p>
  
  <p>I found a way to get some info about my process here: <a href="http://code.activestate.com/recipes/578513-get-memory-usage-of-windows-processes-using-getpro/" rel="nofollow">http://code.activestate.com/recipes/578513-get-memory-usage-of-windows-processes-using-getpro/</a></p>
  
  <p>Putting the code here for convenience:</p>
  
  <pre><code>"""Functions for getting memory usage of Windows processes."""
  
  __all__ = ['get_current_process', 'get_memory_info', 'get_memory_usage']
  
  import ctypes
  from ctypes import wintypes
  
  GetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess
  GetCurrentProcess.argtypes = []
  GetCurrentProcess.restype = wintypes.HANDLE
  
  SIZE_T = ctypes.c_size_t
  
  class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
      _fields_ = [
          ('cb', wintypes.DWORD),
          ('PageFaultCount', wintypes.DWORD),
          ('PeakWorkingSetSize', SIZE_T),
          ('WorkingSetSize', SIZE_T),
          ('QuotaPeakPagedPoolUsage', SIZE_T),
          ('QuotaPagedPoolUsage', SIZE_T),
          ('QuotaPeakNonPagedPoolUsage', SIZE_T),
          ('QuotaNonPagedPoolUsage', SIZE_T),
          ('PagefileUsage', SIZE_T),
          ('PeakPagefileUsage', SIZE_T),
          ('PrivateUsage', SIZE_T),
      ]
  
  GetProcessMemoryInfo = ctypes.windll.psapi.GetProcessMemoryInfo
  GetProcessMemoryInfo.argtypes = [
      wintypes.HANDLE,
      ctypes.POINTER(PROCESS_MEMORY_COUNTERS_EX),
      wintypes.DWORD,
  ]
  GetProcessMemoryInfo.restype = wintypes.BOOL
  
  def get_current_process():
      """Return handle to current process."""
      return GetCurrentProcess()
  
  def get_memory_info(process=None):
      """Return Win32 process memory counters structure as a dict."""
      if process is None:
          process = get_current_process()
      counters = PROCESS_MEMORY_COUNTERS_EX()
      ret = GetProcessMemoryInfo(process, ctypes.byref(counters),
                                 ctypes.sizeof(counters))
      if not ret:
          raise ctypes.WinError()
      info = dict((name, getattr(counters, name))
                  for name, _ in counters._fields_)
      return info
  
  def get_memory_usage(process=None):
      """Return this process's memory usage in bytes."""
      info = get_memory_info(process=process)
      return info['PrivateUsage']
  
  if __name__ == '__main__':
      import pprint
      pprint.pprint(get_memory_info())
  </code></pre>
  
  <p>And this is the result:</p>
  
  <pre><code>{'PageFaultCount': 1942L,
   'PagefileUsage': 4624384L,
   'PeakPagefileUsage': 4624384L,
   'PeakWorkingSetSize': 7544832L,
   'PrivateUsage': 4624384L,
   'QuotaNonPagedPoolUsage': 8520L,
   'QuotaPagedPoolUsage': 117848L,
   'QuotaPeakNonPagedPoolUsage': 8776L,
   'QuotaPeakPagedPoolUsage': 117984L,
   'WorkingSetSize': 7544832L,
   'cb': 44L}
  </code></pre>
  
  <p>But this does not satisfy me. These results give me the whole python process information while what I need is only my specific application that runs on top of the Python framework.</p>
  
  <p>I saw several memory profilers on the internet and also here in Stack Overflow but they are too big for me. The only information that I need is how much memory my app consumes by itself - without taking into account all the Python framework.</p>
  
  <p>How can I achieve this?</p>