Running out of memory in a for loop swift (4)

1.1k Views Asked by At

I'm looping through all pages in a PDFDocument (200+ pages) but app crashes with

Message from debugger: Terminated due to memory issue

The pdf is approx 4mb in size yet each iteration of the loop jumps the memory up approx 30mb. Which doesn't seem right to me. I have managed to locate where in my code the memory is being used just not sure how to claim it back. Tried setting variables to nil but no effect. Tried code in the for loop in an autoreleaspool{} but no effect.

@objc func scrapePDF(){

    let documentURL = self.documentDisplayWebView!.url!
    let document = PDFDocument(url: documentURL)
    let numberOfPages = document!.pageCount

    DispatchQueue.global().async {

        for pageNumber in 1...numberOfPages {

           print(document?.page(at: pageNumber)!.string!)

        }
    }
}

UPDATE: solved ..... kind of

Playing around a bit I found that rather than passing a reference to the PDFDocument inside the loop, if instead I create a new instance for each loop this strangely solves the memory issue. I don't quite understand why though. PDFDocument is a Class not a Struct so is passed by reference. Meaning it is only created once and then referenced to inside my loop. So why would it cause a memory issue?

@objc func scrapePDF(){

    let documentURL = self.documentDisplayWebView!.url!
    let document = PDFDocument(url: documentURL)
    let numberOfPages = document!.pageCount

    DispatchQueue.global().async {

        for pageNumber in 1...numberOfPages {
           let doc = PDFDocument(url: documentURL)
           print(doc?.page(at: pageNumber)!.string!)

        }
    }
}

Though the above code clears the memory issue the problem with it is that its too slow. Each loop takes 0.5 seconds and with 300+ pages I can't accept that. Any tips on speeding it up? Or why it doesn't give the memory back if referencing the PDFDocument from outside the loop

Further UPDATE. It seems that it’s calling the .string method of the PDFPage that is increases the memory to the point of crashing.

0

There are 0 best solutions below