title: Python memoryleak when using numpyarray in classes
body: <p>I'm writing a python-library/package for controlling an oscilloscope. One of the function downloads the traces from the oscilloscope. The data is sent in binary and converted by numpy into a float. In an test_script it will be used in an (almost) infinity loop to save the recorded data(ends when no space is left on the device). But before it can run out of space, the python script runs out of memory. </p>
  
  <p>Here is a broke down version of my library, which suffers the same problem of hoarding memory without releasing it, even though being told to free it:</p>
  
  <pre><code>import time
  import gc
  import numpy
  from pympler import muppy, summary
  
  class Measurement:
  
      data = None
  
      def __del__(self):
          del self.data
          print("Deleting Measurement")
  
  class Oscilloscope:
  
      def GetData(self):
          # Simulating the acquiration of the data by reading it in by a file
          p = open(r"rawdata.bin",mode="rb")
          rawdata = p.read()
          mes = Measurement()
          mes.data = numpy.array(numpy.frombuffer(bytes(rawdata), dtype='B'), dtype=float)
          p.close()
          return mes
  
      def __del__(self):
          print("Deleting Oscilloscope")
  
  
  if __name__ == "__main__":
      myosci = Oscilloscope()
      for i in range(0,10):
          var = myosci.GetData()
          data = var.data
  
          # In the real script something will be done with the data
          print("Saving and evaluating Traces")
          time.sleep(0.5)
  
          del var
          gc.collect()
          print("Collected everything")
          # Show MemoryUsage
          all_objects = muppy.get_objects()
          sum1 = summary.summarize(all_objects)
          # Prints out a summary of the large objects
          summary.print_(sum1)
      print("Done")
  </code></pre>
  
  <p>When executed the output looks like this:</p>
  
  <pre><code>Saving and evaluating Traces
  Deleting Measurement
  Collected everything
                         types |   # objects |   total size
  ============================ | =========== | ============
                 numpy.ndarray |          40 |      9.16 MB
                           str |       18144 |      3.39 MB
                          dict |        4685 |      2.21 MB
                          code |        6556 |    925.30 KB
                          type |        1060 |    868.78 KB
                         tuple |        4209 |    272.90 KB
            wrapper_descriptor |        2209 |    172.58 KB
                           set |         139 |    131.41 KB
                       weakref |        1339 |    104.61 KB
             method_descriptor |        1402 |     98.58 KB
                          list |         396 |     88.62 KB
    builtin_function_or_method |        1165 |     81.91 KB
                   abc.ABCMeta |          84 |     81.50 KB
             getset_descriptor |        1050 |     73.83 KB
                           int |        1994 |     60.12 KB
  Saving and evaluating Traces
  Deleting Measurement
  Collected everything
                         types |   # objects |   total size
  ============================ | =========== | ============
                 numpy.ndarray |          41 |     18.31 MB
                           str |       22639 |      3.70 MB
                          dict |        4685 |      2.21 MB
                          list |        4895 |    959.05 KB
                          code |        6556 |    925.30 KB
                          type |        1060 |    868.78 KB
                         tuple |        4212 |    273.09 KB
            wrapper_descriptor |        2209 |    172.58 KB
                           set |         139 |    131.41 KB
                       weakref |        1339 |    104.61 KB
             method_descriptor |        1402 |     98.58 KB
                           int |        2940 |     85.98 KB
    builtin_function_or_method |        1165 |     81.91 KB
                   abc.ABCMeta |          84 |     81.50 KB
             getset_descriptor |        1050 |     73.83 KB
  Saving and evaluating Traces
  Deleting Measurement
  Collected everything
                         types |   # objects |   total size
  ============================ | =========== | ============
                 numpy.ndarray |          42 |     27.47 MB
                           str |       27134 |      4.00 MB
                          dict |        4685 |      2.21 MB
                          list |        9392 |      1.90 MB
                          code |        6556 |    925.30 KB
                          type |        1060 |    868.78 KB
                         tuple |        4212 |    273.09 KB
            wrapper_descriptor |        2209 |    172.58 KB
                           set |         139 |    131.41 KB
                           int |        3879 |    111.66 KB
                       weakref |        1339 |    104.61 KB
             method_descriptor |        1402 |     98.58 KB
    builtin_function_or_method |        1165 |     81.91 KB
                   abc.ABCMeta |          84 |     81.50 KB
             getset_descriptor |        1050 |     73.83 KB
  Saving and evaluating Traces
  Deleting Measurement
  Collected everything
                         types |   # objects |   total size
  ============================ | =========== | ============
                 numpy.ndarray |          43 |     36.63 MB
                           str |       31629 |      4.31 MB
                          list |       13889 |      2.94 MB
                          dict |        4685 |      2.21 MB
                          code |        6556 |    925.30 KB
                          type |        1060 |    868.78 KB
                         tuple |        4212 |    273.09 KB
            wrapper_descriptor |        2209 |    172.58 KB
                           int |        4818 |    137.34 KB
                           set |         139 |    131.41 KB
                       weakref |        1339 |    104.61 KB
             method_descriptor |        1402 |     98.58 KB
    builtin_function_or_method |        1165 |     81.91 KB
                   abc.ABCMeta |          84 |     81.50 KB
             getset_descriptor |        1050 |     73.83 KB
  Saving and evaluating Traces
  Deleting Measurement
  Collected everything
                         types |   # objects |   total size
  ============================ | =========== | ============
                 numpy.ndarray |          44 |     45.78 MB
                           str |       36124 |      4.62 MB
                          list |       18386 |      4.05 MB
                          dict |        4685 |      2.21 MB
                          code |        6556 |    925.30 KB
                          type |        1060 |    868.78 KB
                         tuple |        4212 |    273.09 KB
            wrapper_descriptor |        2209 |    172.58 KB
                           int |        5757 |    163.01 KB
                           set |         139 |    131.41 KB
                       weakref |        1339 |    104.61 KB
             method_descriptor |        1402 |     98.58 KB
    builtin_function_or_method |        1165 |     81.91 KB
                   abc.ABCMeta |          84 |     81.50 KB
             getset_descriptor |        1050 |     73.83 KB
  Saving and evaluating Traces
  Deleting Measurement
  Collected everything
                         types |   # objects |   total size
  ============================ | =========== | ============
                 numpy.ndarray |          45 |     54.94 MB
                          list |       22883 |      5.25 MB
                           str |       40619 |      4.92 MB
                          dict |        4685 |      2.21 MB
                          code |        6556 |    925.30 KB
                          type |        1060 |    868.78 KB
                         tuple |        4212 |    273.09 KB
                           int |        6696 |    188.69 KB
            wrapper_descriptor |        2209 |    172.58 KB
                           set |         139 |    131.41 KB
                       weakref |        1339 |    104.61 KB
             method_descriptor |        1402 |     98.58 KB
    builtin_function_or_method |        1165 |     81.91 KB
                   abc.ABCMeta |          84 |     81.50 KB
             getset_descriptor |        1050 |     73.83 KB
  Saving and evaluating Traces
  Deleting Measurement
  Collected everything
                         types |   # objects |   total size
  ============================ | =========== | ============
                 numpy.ndarray |          46 |     64.09 MB
                          list |       27380 |      6.55 MB
                           str |       45114 |      5.23 MB
                          dict |        4685 |      2.21 MB
                          code |        6556 |    925.30 KB
                          type |        1060 |    868.78 KB
                         tuple |        4212 |    273.09 KB
                           int |        7635 |    214.36 KB
            wrapper_descriptor |        2209 |    172.58 KB
                           set |         139 |    131.41 KB
                       weakref |        1339 |    104.61 KB
             method_descriptor |        1402 |     98.58 KB
    builtin_function_or_method |        1165 |     81.91 KB
                   abc.ABCMeta |          84 |     81.50 KB
             getset_descriptor |        1050 |     73.83 KB
  Saving and evaluating Traces
  Deleting Measurement
  Collected everything
                         types |   # objects |   total size
  ============================ | =========== | ============
                 numpy.ndarray |          47 |     73.25 MB
                          list |       31877 |      7.85 MB
                           str |       49609 |      5.54 MB
                          dict |        4685 |      2.21 MB
                          code |        6556 |    925.30 KB
                          type |        1060 |    868.78 KB
                         tuple |        4212 |    273.09 KB
                           int |        8574 |    240.04 KB
            wrapper_descriptor |        2209 |    172.58 KB
                           set |         139 |    131.41 KB
                       weakref |        1339 |    104.61 KB
             method_descriptor |        1402 |     98.58 KB
    builtin_function_or_method |        1165 |     81.91 KB
                   abc.ABCMeta |          84 |     81.50 KB
             getset_descriptor |        1050 |     73.83 KB
  Saving and evaluating Traces
  Deleting Measurement
  Collected everything
                         types |   # objects |   total size
  ============================ | =========== | ============
                 numpy.ndarray |          48 |     82.40 MB
                          list |       36374 |      9.26 MB
                           str |       54104 |      5.84 MB
                          dict |        4685 |      2.21 MB
                          code |        6556 |    925.30 KB
                          type |        1060 |    868.78 KB
                         tuple |        4212 |    273.09 KB
                           int |        9513 |    265.71 KB
            wrapper_descriptor |        2209 |    172.58 KB
                           set |         139 |    131.41 KB
                       weakref |        1339 |    104.61 KB
             method_descriptor |        1402 |     98.58 KB
    builtin_function_or_method |        1165 |     81.91 KB
                   abc.ABCMeta |          84 |     81.50 KB
             getset_descriptor |        1050 |     73.83 KB
  Saving and evaluating Traces
  Deleting Measurement
  Collected everything
                         types |   # objects |   total size
  ============================ | =========== | ============
                 numpy.ndarray |          49 |     91.56 MB
                          list |       40871 |     10.79 MB
                           str |       58599 |      6.15 MB
                          dict |        4685 |      2.21 MB
                          code |        6556 |    925.30 KB
                          type |        1060 |    868.78 KB
                           int |       10452 |    291.39 KB
                         tuple |        4212 |    273.09 KB
            wrapper_descriptor |        2209 |    172.58 KB
                           set |         139 |    131.41 KB
                       weakref |        1339 |    104.61 KB
             method_descriptor |        1402 |     98.58 KB
    builtin_function_or_method |        1165 |     81.91 KB
                   abc.ABCMeta |          84 |     81.50 KB
             getset_descriptor |        1050 |     73.83 KB
  Done
  Deleting Oscilloscope
  
  Process finished with exit code 0
  </code></pre>
  
  <p>The memory is hoarded even though the numpyarrays receive a delete command and the garbage collector gets triggered. In the real script the memory-usage jumps with every step by around 150 mB. The Raspberry Pi which is running my library gets out of memory pretty quick. </p>
  
  <p>If you want to test it yourself, here is the rawdata.bin file used:
  <a href="https://mega.nz/#!ps4ACAJD!0cqTJJmMU5RjSx5BqggM1afz47PqcR67hzKKnI8LgTs" rel="nofollow noreferrer">https://mega.nz/#!ps4ACAJD!0cqTJJmMU5RjSx5BqggM1afz47PqcR67hzKKnI8LgTs</a></p>