"Too many files open" issue used by FMDatabase/SQLite3
Asked Answered
K

2

6

I am using the FMDatabase wrapper for SQLite3 in my OSX App. I do tons of inserts into the database:

FMResultSet *results;
results= [db executeQuery:@"select count(*) from `items` where key = ?",[keyPath lowercaseString],nil];

while([results next])
{
    if([results unsignedLongLongIntForColumnIndex:0]>0){
        updateOperation=TRUE;
    }
}
[results close];

if(updateOperation){

    [db executeUpdate:@"update `items` set OSXsize=?,OSXOsize=?, OSXDate=?, UUID=?,sourceFile=?,tombStone=0,SandBoxBookMark=?,songname=?,albumartist=? where key=?",
     size,originalSize, convertedDate,UUID,sourcePath,bookmark,fileName,albumArtist,[keyPath lowercaseString] , nil];
}
else
{
    [db executeUpdate:@"insert into `items`(key,filepath, OSXsize, OSXOsize, OSXdate,UUID,sourceFile,tombStone,SandBoxBookMark,songname,albumartist) values(?,?,?,?,?,?,?,0,?,?,?)",
     [keyPath lowercaseString], dapPath, size,originalSize, convertedDate,UUID,sourcePath,bookmark,fileName,albumArtist, nil];
}

I open the database once, however, I am seeing 4725+file handles attached as the app progresses in activity monitor:

/Users/userA/Library/Containers/com.map-pin.Dapper/Data/Library/Application Support/com.map-pin.Dapper/dapperright.sqlite
15
16
...
4724
4725
Kepi answered 18/5, 2017 at 14:50 Comment(0)
A
0

Is this in a multithread app and do you create and open a db instance every time you update/insert a row? Do you have [db close]; somewhere?

Have you tested FMDatabaseQueue?

Astigmia answered 1/6, 2017 at 11:6 Comment(1)
Using fmdatabasequeue turned on some logging which revealed a query result set which was not properly closedKepi
L
-1

For the user that runs your database, run the following command:

limit maxfiles 4096 16384 

Afterwards restart the database. Now it should be able to handle 16k files.

How you persist this for OS X reboots depends on the version of OS X. You will find it by searching in your favorite search engine

Lindon answered 1/6, 2017 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.