【问题标题】:"Too many files open" issue used by FMDatabase/SQLite3FMDatabase/SQLite3 使用的“打开的文件太多”问题
【发布时间】:2017-05-18 14:50:20
【问题描述】:

我在我的 OSX 应用程序中使用 SQLite3 的 FMDatabase 包装器。我在数据库中进行了大量插入:

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];
}

我打开数据库一次,但是,随着应用程序在活动监视器中的进展,我看到附加了 4725+ 个文件句柄:

/Users/userA/Library/Containers/com.map-pin.Dapper/Data/Library/Application Support/com.map-pin.Dapper/dapperright.sqlite
15
16
...
4724
4725

【问题讨论】:

    标签: xcode macos sqlite fmdb


    【解决方案1】:

    这是在多线程应用程序中,您是否在每次更新/插入一行时创建并打开一个 db 实例?你在某处有[db close];吗?

    你测试过FMDatabaseQueue吗?

    【讨论】:

    • 使用 fmdatabasequeue 打开了一些日志记录,显示查询结果集未正确关闭
    【解决方案2】:

    对于运行数据库的用户,运行以下命令:

    limit maxfiles 4096 16384 
    

    然后重新启动数据库。现在它应该可以处理 16k 个文件了。

    如何在 OS X 重新启动时坚持这一点取决于 OS X 的版本。您可以通过在您最喜欢的搜索引擎中搜索找到它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 2012-05-09
      • 2011-07-18
      • 2019-10-30
      相关资源
      最近更新 更多