0

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)
    }
  }
Avba
  • 14,822
  • 20
  • 92
  • 192
  • Have a look at https://stackoverflow.com/q/25860942/1187415. – Martin R Jun 26 '19 at 11:36
  • doesn't help to wrap it in an autoreleasepool. memory still grows – Avba Jun 26 '19 at 11:45
  • Can you show your code with the autorelease pool? – Martin R Jun 26 '19 at 11:47
  • same but wrapped in the loop autoreleasepool { lines.forEach { (s) in request(s: s) } } – Avba Jun 26 '19 at 11:47
  • That's what I assumed :) – The autorelease pool should be *inside* the forEach loop. – Martin R Jun 26 '19 at 11:48
  • chagned to be lines.forEach { (s) in autoreleasepool { request(s: s) } } but still memory is seems to be growing – Avba Jun 26 '19 at 11:49
  • You should use a real URL session defined once before your code. Do not clog the shared session. – matt Jun 26 '19 at 11:59
  • I'm just running a simple load test to a service from a throw away tool, I just noticed that the memory keeps growing and that eventually will crash – Avba Jun 26 '19 at 12:01
  • Seems that the autorelease pool is working, just need some patience for it to kick in (took a couple of minutes it seems) @MartinR – Avba Jun 26 '19 at 12:05

0 Answers0