我在swift中有一个while循环,它试图解决一个问题,有点像比特币挖掘。简化版是-
import SwiftyRSA
func solveProblem(data: String, complete: (UInt32, String) -> Void) {
let root = data.sha256()
let difficulty = "00001"
let range: UInt32 = 10000000
var hash: String = "9"
var nonce: UInt32 = 0
while (hash > difficulty) {
nonce = arc4random_uniform(range)
hash = (root + String(describing: nonce)).sha256()
}
complete(nonce, hash)
}
solveProblem(data: "MyData") { (nonce, hash) in
// Problem solved!
}
当这个循环运行时,内存使用量将稳定地clime,有时会达到~300MB,一旦完成,它似乎不会被释放。
有人能解释为什么会这样,如果这是我应该担心的事情吗?