'Too many open files' error

2019-05-31 00:34发布

In my app, there is an insert operation on a form submission. Most of the cases the insert operation is successful. Sometimes the insertion operation is not happening and then , it is giving java.lang.RuntimeException. Here is the logcat details:

03-28 10:52:09.260: ERROR/IMemory(1501): cannot dup fd=1023, size=1048576, err=0 (Too many open files)
03-28 10:52:09.260: ERROR/IMemory(1501): cannot map BpMemoryHeap (binder=0x5919b0), size=1048576, fd=-1 (Bad file number)
03-28 10:52:09.260: ERROR/JavaBinder(1501): *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
03-28 10:52:09.260: ERROR/JavaBinder(1501): java.lang.RuntimeException: No memory in memObj
03-28 10:52:09.260: ERROR/JavaBinder(1501):     at android.database.CursorWindow.native_init(Native Method)
03-28 10:52:09.260: ERROR/JavaBinder(1501):     at android.database.CursorWindow.<init>(CursorWindow.java:518)
03-28 10:52:09.260: ERROR/JavaBinder(1501):     at android.database.CursorWindow.<init>(CursorWindow.java:27)
03-28 10:52:09.260: ERROR/JavaBinder(1501):     at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:493)
03-28 10:52:09.260: ERROR/JavaBinder(1501):     at android.database.CursorWindow$1.createFromParcel(CursorWindow.java:496)
03-28 10:52:09.260: ERROR/JavaBinder(1501):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:96)
03-28 10:52:09.260: ERROR/JavaBinder(1501):     at android.os.Binder.execTransact(Binder.java:287)
03-28 10:52:09.260: ERROR/JavaBinder(1501):     at dalvik.system.NativeStart.run(Native Method)

What may be the problem in my code?

1条回答
啃猪蹄的小仙女
2楼-- · 2019-05-31 00:59

Very often (as trjanfoe suggested) these sorts of problems are because Cursors are still open. I've found that I can eliminate most of the problems by use try/finally codeblocks.

Cursor cursor = null;
try {
    cursor = db.insert(...);
    more code here
}
finally {
    if (cursor != null) {
        cursor.close();
        cursor = null;
    }
}

This ensures that the cursor will ALWAYS be closed, no matter what happens.

查看更多
登录 后发表回答