memory usage increases dramatically after each FMD

2019-07-25 13:51发布

Below is my source code, every time I execute the function, the memory usage increases dramatically. Please help to point out what is the problem.

func loadfontsFromDatabase(code:String)->[String] {
    let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0] as AnyObject
    let databasePath = documentsPath.appending("/bsmcoding.sqlite")
    let contactDB = FMDatabase(path: databasePath as String)
    var c:[String]=[]

    let querySQL = "SELECT FONT FROM BSMCODE WHERE BSMCODE.CODE = '\(code)' ORDER BY NO DESC"

    NSLog("query:\(querySQL)")

    let results:FMResultSet? = Constants.contactDB?.executeQuery(querySQL, withArgumentsIn: nil)

    while (results?.next())! {
        c.append((results?.string(forColumn: "FONT"))!)
    }

    results?.close()

    return c
}

1条回答
姐就是有狂的资本
2楼-- · 2019-07-25 14:15

There's nothing here that would account for any substantial memory loss. I would suggest using the "Debug Memory Graph" feature in Xcode 8 to identify what objects are being created and not being released, but I suspect the problem rests elsewhere in your code. Or use Instruments to track it down what's leaking and debug from there. See https://stackoverflow.com/a/30993476/1271826.

There are unrelated issues here, though:

  1. You are creating local contactDB, but you never open it and you never use it. It will be released when the routine exits, but it's completely unnecessary if you're going to use Constants.contactDB, anyway.

  2. I'd advise against using string interpolation when building your SQL. Use ? placeholder and pass the code in as a parameter. This is much safer, in case the code ever contained something that couldn't be represented in SQL statement. (This is especially true if the code was supplied by the user, in which case you'd be susceptible to SQL injection attacks or innocent input errors that could lead to crashes.)

    For example, you could do something like:

    func loadfontsFromDatabase(code: String) -> [String] {
        var c = [String]()
    
        let querySQL = "SELECT FONT FROM BSMCODE WHERE BSMCODE.CODE = ? ORDER BY NO DESC"
    
        let results = try! Constants.contactDB!.executeQuery(querySQL, values: [code])
    
        while results.next() {
            c.append((results.string(forColumn: "FONT"))!)
        }
    
        return c
    }
    

    If you don't like the forced unwrapping, you can do optional unwrapping if you want, but personally I'd rather know immediately when debugging during the development phase if there's some logic mistake (e.g. the contactDB wasn't open, the SQL is incorrect, etc.). But you can do optional binding and add the necessary guard statements if you want. But don't just do optional binding and silently return a value suggesting that everything is copacetic, leaving you with a debugging challenge of tracking down the problem if you don't get what you expected.

    But the key point is to avoid inserting values into your SQL directly. Use ? placeholders.

查看更多
登录 后发表回答