Thursday, September 13, 2007

too many open files

so i've just been dealing with too many open file descriptors in matlab. i called tech support and they told me that they don't normally deal with customers who have just a student license. i can't see the source of matlab's imread() function, so i don't know for sure that they aren't leaving a file open in there somewhere. the code that generates this error looks like this:
for ii = 1 : size( picPaths, 2);
picElems(ii) = picElem(picPaths{ii});
end;
picElem( path) will create a picElem object with useful fields (such as rows, and cols). to do so, it uses imread() which must open the file. picPaths holds thousands of paths by the time this part of the code is reached. i can't narrow down exactly where files are being opened. the code above it has a bunch of calls to dir() which might be leaving files open. unfortunately i don't know how to get a list of currently open files, so it's hard to narrow down where files are being left open. maybe i could do something with lsof on the command line ... that doesn't sound too appetizing.
i went through several temporary fixes for this problem:
  1. my first fix was to limit the number of files actually opened. i called picElem 1 out of 10 times reached it. this fix allowed me to run the function more times and generate less detailed pictures, with 1/10th the number of data points.
  2. the second fix was one i got from the tech support guy. i can call fclose('all') to close all open files. calling this just after calling picElem, or just before the loop works. it allows me to generate full detailed graphs. it does close every open file though. i can imagine myself wanting to keep some files open at some point. for instance, the diary() file.
neither of these are complete fixes. if there were some way to get a listing of every open file, i could more quickly narrow down where files are left open.

No comments: