0

I'm processing wav files and checking each for energy levels using python's wave module. Everything works great for dozens of files but then suddenly I start to get this MemoryError exceptions. For the meanwhile I ignore the exception and continue with the checks which sometimes fail with exception and sometimes gives me the answer.

The method that checks the files goes like this (Please focus at the MemoryError exception as my code works though it's not accessible to me at the moment so I've decided to rewrite it's basics):

import wave,os,glob

def wavCheck(filepath):
    with open(filepath,'rb') as w:
        handle = wave.open(w)
        data = handle.readframes()
        nframes = handle.getnframes()
        channels = [[] for x in handle.getchannels()]
        for i in nframes:
            bucket = i%2
            channels[bucket].append(data[i])
        for channel in channels:
            # calculate which frame is silent and do something
        handle.close()
        del handle
        del nframes
        del channels
    return results

if __name__ == "__main__":
    while os.path.exists(someDirectoryWhichContainWaves):
        for filepath in glob.glob(someDirectoryWhichContainWaves+'\*.wav')
            results = wavCheck(filepath)
            # Do something with results

Well - assuming that reading the file is done ok and everything goes well and as expected and of course that the wave files are all ok, why would I get MemoryError exception?

Extra details -

  1. Wave files are between 3MBytes and 10MBytes
  2. I've been trying to customize Garbage Collector to collect every few iterations and this didn't work. After doing some reading about GC, I decided that the best way would be to check whether there is garbage to collect at all. So I did. This didn't work either.

Please- any ideas here?

Cheers.

devdc
  • 161
  • 1
  • 4
  • 13
  • 1
    Does closing the handle help at all? `handle.close()` at the end of the function. – Martijn Pieters Oct 28 '12 at 12:11
  • Hey Martijn. I did close the handle and that did not help. I'll edit my post. I'd love to hear of more thoughts. – devdc Oct 28 '12 at 12:27
  • In which statement exactly are you getting the `MemoryError` exception? That may give us a clue. – Pedro Romano Oct 28 '12 at 12:31
  • Hey Pedro. Thanks for your help. I don't know on which statement. The exception pops clear. Just MemoryError. Is there a way in which I can debug and check on which statement the exception occurs? – devdc Oct 28 '12 at 12:35
  • Pedro - None. Just `MemoryError` and python quits (kills my script). – devdc Oct 28 '12 at 12:42
  • I recommend going with a strategy where you only load a few frames in memory at a time and process those straight away without loading the whole file. By the way, the rewritten example you posted is incorrect, because `getnframes` just returns the number of frames not the frames themselves. – Pedro Romano Oct 28 '12 at 13:54
  • Hey Pedro. I will try your recommendation for loading few frames. About your next comment- I know. I used data and not frame. I'll edit that. Would love to hear more specific MemoryError ideas. Thanks. – devdc Oct 28 '12 at 14:25
  • You return `results` but I don't see where you are defining it? If this is not the exact working code then I'm not sure how to help debug the problem. You must be accumulating references to an object somewhere which means the garbage collector can't do it's job. You might be able to use it's api to help figure it out. http://docs.python.org/2/library/gc.html – aychedee Oct 28 '12 at 18:00
  • I tried to `w.close()` the with statement which did not help. Unfortunately, reading only few frames is too difficult since I unpack the wave my self. I'll bring up the fully original code in few days. If anyone's got any ideas by then, it'd be appreciated. – devdc Nov 09 '12 at 20:43

0 Answers0