This Swift code (based on another post here on Stack Overflow) computes ten million MD5 hashes (and does nothing with them, for the sake of this example). But the md5() function leaks memory; the longer it runs, the more memory it consumes:
import Foundation
func md5(string: String) -> [UInt8] {
var digest = [UInt8](count: Int(CC_MD5_DIGEST_LENGTH), repeatedValue: 0)
if let data = string.dataUsingEncoding(NSUTF8StringEncoding) {
CC_MD5(data.bytes, CC_LONG(data.length), &digest)
}
return digest
}
for var i = 0; i < 10000000; i++ {
let hash = md5(String(format:"%u", i))
}
What is it about this md5() function that's consuming memory and not freeing it automatically? Is there anything I can/should do in code to cause it to free memory it no longer needs?