There is a minimal application on Swift (a test application for locating the problem mostly):

import UIKit import Alamofire class ViewController: UIViewController { let request = AskServer() @IBAction func pressTheButton(_ sender: Any) { AskServer.sendRequest() } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } class AskServer: NSObject { class func sendRequest() { let param: Parameters = ["X1": "12345","X2":"1","X3": "1","X4": "1", "X5": "0.0", "X6": "0", "X7": "g"] let myHost = "http://XXX.XXX.XXX.XXX" Alamofire.request(myHost, method: .post, parameters: param, encoding: JSONEncoding.default).responseJSON{response in } } } 

by clicking on the button, the server is queried using the Alamofire library. The call fulfills correctly, literally in a fraction of a second. But then about 10-20 seconds, the application eats the processor up to 70%. Accordingly, the battery on the iPhone 6Plus melts before our eyes.

If you comment out Alamofire.request - everything is fine, processor load, God forbid, 1% when you press the button.

What can so consume resources? Help to localize the problem !!!

  • XCode Insturment used, does not show who eats battery. Perhaps it was not quite right, because did it for the first time. - kott

1 answer 1

Try the proven library for AFNetworking network requests. I can also advise you to observe the memory growth after the request. If it stops at some value - perhaps somewhere in the library there is a problem, if it grows continuously and falls after didReceiveMemoryWarning - there is also a problem somewhere you have - a memory leak.

  • if I understand correctly - AFNetworking is the same Alamofire, only in Objective-C. Those. does not fit at least in the parameter that I write on Swift. I have a problem with a leak - well, so look at the code that I brought, where can there be a leak? There is no other code! - kott
  • 1. objective-c compiles with swift without problems, if that 2. you have a simple query. Try writing it with NSURLSession 3. So you didn’t answer, does the memory grow all the time or does it stop somewhere? - maxonflic
  • 1. Well, to cross Swift and Objective-C, you must at least learn Objective-C, I will not zan it. Thanks anyway! 3. When the application is loaded, the consumption of memory consumption is 4.3 megabytes; when the request is executed, the growth will not exceed 5.3 megabytes and then it will not fall. With repeated requests it keeps on 5.3 to infinity - kott
  • NSURLSession, rewrite your request in the native component. It is fast. If memory returns to normal, the problem is in Alamofire. Good luck - maxonflic
  • on URLSession - the same picture! The call fulfills instantly. Then seconds 10 Energy Impact rolls over! - kott