Hi Philipp,
OK, here’s a rundown of what I’ve learned about this issue:
The biggest problem with the MATLAB getEvents function was that it basically doubled its memory usage by keeping two copies of each event in memory. (One of the copies was properly deleted when the function returned, so while it certainly wasted memory, it didn’t actually leak any.) This is fixed in the current nightly build. The function now keeps only one copy of each event, so it only needs (approximately) as much RAM as is needed to hold the returned events array.
A more fundamental problem is the fact that getEvents returns all the events at once, in a single array. Regardless of how efficiently the function uses memory, it’s still going to be possible to exceed available RAM simply by reading a huge number of events. In the Python bindings, I’ve added a path around this issue by providing a method (get_events_iter) that returns an iterator over the requested events, which reads them into memory one at a time. While we can do something similar in MATLAB, it will take a bit more work – probably I’ll need to create a MATLAB equivalent to the Python MWKFile class, with methods to start iteration and to return events one by one.
The question of whether the current approach (where all the events must be in RAM simultaneously) will work for you depends on how many events you want to read and how large the typical event’s payload is. In my testing (using 64-bit MATLAB R2010a), I’ve found that a 500MB file containing ~1,000,000 events (most of which are random dots draw announcements, without the per-frame dot positions) consumes about 3.8GB of RAM when read in to MATLAB. A 1GB file with twice as many events would presumably consume about twice as much memory, which would be murder on my Mac Pro with 8GB of RAM but would probably run pretty smoothly with 16GB.
I’d appreciate it if you could download the latest nightly and try loading your 1GB file. Hopefully it will load smoothly, but if you still end up in swapping hell, then I’ll need to implement event-by-event iteration sooner rather than later.
One final note: I believe your observation about the memory used by the events array not being released is actually the result of some quirky memory-management behavior by MATLAB, rather than a memory leak. Specifically, I’ve noticed that if I load a large number of events into the array events, and then I assign a new value to the variable, e.g.
events = 0;
then MATLAB’s memory usage does not decrease. However, if I use the clear command on the variable, i.e.
clear events
then the memory is released. My guess is that MATLAB is being conservative about destroying arrays that are themselves embedded in structure or cell arrays, deferring any action until it can do a thorough scan of allocated memory to ensure that all references to such arrays are gone. I could be completely wrong, of course, but you might want to see if clear does the trick for you, too.
Cheers,
Chris