I'm running requests in a loop and see that the memory usage is growing unbounded
I think that the URLSessionDataTask
objects aren't released by the URLSession
My code looks like this
let base = "http://myserver.com"
let group = DispatchGroup()
// 10 requests in parallel
let semaphore = DispatchSemaphore(value: 10)
func request(s: String) {
var c = URLComponents(string: base)!
c.queryItems = [URLQueryItem(name: "uid", value: s)]
let url = c.url!
var request = URLRequest(url: url)
let dataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
semaphore.signal()
if let data = data {
let json = String(data: data, encoding: .utf8)
print("\(url) \(String(describing: json))")
}
}
semaphore.wait()
dataTask.resume()
}
// file with million ids
let fileName = "ids.txt"
let contents = try String(contentsOfFile: fileName)
let lines = contents.components(separatedBy: .newlines)
lines.forEach { (s) in
request(s: s) // make a request for each
}
EDIT
Per comments I tried to wrap with autorelease
but still have memory growth
lines.forEach { (s) in
autoreleasepool {
request(s: s)
}
}