0

The app I am making is almost complete, however I am seeing a memory increase every 15 seconds until the app crashes. It increases ~10mb/15seconds.

The app is pretty simple.

I call a repeating timer in applicationDidFinishLaunching as shown here:

    timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "refreshIpAddresses", userInfo: nil, repeats: true)

the timer calls a function called refreshIpAddresses() which calls 3 more functions. This is show below:

func refreshIpAddresses() {
    getPrivateIp()
    getIpData()
    getDnsData()
}

Each of these are similar, so I will just demo one of them. The code below is the method getDnsData().

func getDnsData() {
    Alamofire.request(.GET, dataSourceURL2!)
        .responseJSON { response in

            if (response.result.value != nil) {
            let json2 = JSON(response.result.value!)
            let json3 = json2["dns"]
            let geo = json3["geo"].stringValue
            let ip = json3["ip"].stringValue

            self.dnsIp.title = "DNS IP: " + ip
            self.dnsName.title = "DNS Name: " + geo
            } else {
                self.dnsIp.title = "No Internet Connection"
                self.dnsName.title = "No Internet Connection"
            }
    }
}

If I comment out the timers, the memory increase stops being an issue.

Is my timer somehow storing information every loop? Should it be invalidated somewhere? Is it my getDnsData() method?

Please help.

EDIT It might not be due to the NSTimer. Can someone guide me how to figure out what the issue is?

Here is the memory leak checks in instruments: enter image description here

bandoy123
  • 253
  • 4
  • 12
  • What does `JSON()` do? – Jeffery Thomas Nov 29 '15 at 04:13
  • It comes from SwiftyJSON's library. It gets data from a JSON array. See the Usage section here https://github.com/SwiftyJSON/SwiftyJSON – bandoy123 Nov 29 '15 at 06:00
  • I don't believe the problem rests in this code sample. It's likely something else. I also would focus on the jump in allocations, not the "leaks" graph. The jumps in allocations are large, so I'd select a short range (by dragging a time range right before and right after that point) and then look at the allocations over that period of time. You can then identify what those allocations are and back track to where these were allocated (assuming you've left the "record reference counts" feature turn on in Instruments). E.g. http://stackoverflow.com/a/14105056/1271826 – Rob Nov 29 '15 at 06:12
  • Hmmmm okay. If you have any time, here is the instruments.trace https://drive.google.com/open?id=0Bw2GWtsm8zaSaWYwSjdzWUcyTG8 – bandoy123 Nov 29 '15 at 06:39

0 Answers0