Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Why is referencing an object inside a loop causing memory crash

ryantcbryantcb Posts: 667Registered Users @ @ @
edited December 2017 in iOS SDK Development
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.
@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!)
                
            }
        }
    }


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
Sign In or Register to comment.