Minimize Open Files on External Storage, Please

There is some evidence to suggest that on some devices, there is a limit of 1024 simultaenously open file descriptors on external storage. This was mentioned in an android-developers post, which leads to this issue.

What you will see, when you hit the limit, is a FileNotFoundException triggered by an EMFILE error:

11-08 15:57:46.915: E/OpenLotsaFiles2(17398): Exception reading file 11-08 15:57:46.915: E/OpenLotsaFiles2(17398): java.io.FileNotFoundException: /storage/sdcard0/... (Too many open files) 11-08 15:57:46.915: E/OpenLotsaFiles2(17398): at libcore.io.IoBridge.open(IoBridge.java:416) 11-08 15:57:46.915: E/OpenLotsaFiles2(17398): at java.io.FileInputStream.<init>(FileInputStream.java:78) 11-08 15:57:46.915: E/OpenLotsaFiles2(17398): at com.commonsware.android.olf2.MainActivity... 11-08 15:57:46.915: E/OpenLotsaFiles2(17398): Caused by: libcore.io.ErrnoException: open failed: EMFILE (Too many open files) 11-08 15:57:46.915: E/OpenLotsaFiles2(17398): at libcore.io.Posix.open(Native Method) 11-08 15:57:46.915: E/OpenLotsaFiles2(17398): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 11-08 15:57:46.915: E/OpenLotsaFiles2(17398): at libcore.io.IoBridge.open(IoBridge.java:400) 11-08 15:57:46.915: E/OpenLotsaFiles2(17398): ... 2 more

Please make sure that you are modest in how many open files you have on external storage at once. It is very possible for there to be quite a few running processes on an Android device nowadays, and if many of them have many open external storage files, apps will start failing with these sorts of errors.

Many thanks to Elliot Hughes for pointing out a flaw in my original test, leading me to write a better test, which has been uploaded to the issue.

Need an extra hand with your Android app development project? CommonsWare can help — reach out for details!

— Nov 08, 2012