Message from debugger: Terminated due to memory issue

前端 未结 6 1604
小蘑菇
小蘑菇 2021-02-18 21:52

My app working with Geojson file. I use MapBox SDK to add MGLPolyline to map. But the problem is my file too large, so that the app crash and got the e

6条回答
  •  感动是毒
    2021-02-18 22:38

    One thing I have learnt from creating memory intensive apps is that you have to use autoreleasepool every time you create variables inside loops, if these loops are long

    Review all your code and transform things like

    func loopALot() {
        for _ in 0 ..< 5000 {
            let image = NSImage(contentsOfFile: filename)
        }
    }
    

    into

    func loopALot() {
        for _ in 0 ..< 5000 {
          autoreleasepool {
            let image = NSImage(contentsOfFile: filename)
          }
        }
    }
    

    Review all kinds of loops for, while, etc.

    This will force of iOS to release the variable and its correspondent memory usage at the end of every turn of the loop, instead of holding the variable and its memory usage until the function ends. That will reduce dramatically your memory usage.

提交回复
热议问题