I have a while loop in Swift that's attempting to solve a problem, a bit like Bitcoin mining. A simplified version is -
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!
}
Whilst this loop is running the memory usage will steadily clime sometimes reaching ~300mb, and once complete it doesn't seem to be released.
Would someone be able to explain why this is the case and if it's something I should be worrying about?