【发布时间】:2018-02-23 23:45:24
【问题描述】:
编辑了这个问题以使用更简单的代码版本。 TestPDF 全是文本,大约 300 页。当循环运行时,它会在消耗 2gb 内存后崩溃。打印后我不需要 print 语句中的值。但是,代码将其保存在内存中。如何在循环关闭前清除打印语句内容的内存分配?
func loadPDFDocument(){
let documentURL = Bundle.main.url(forResource: "TestPDF", withExtension: "pdf")!
if let document = PDFDocument(url: documentURL) {
for page in 1...document.pageCount {
DispatchQueue.global().async {
print(document.page(at: page)!.string!)
}
}
}
}
我尝试过的解决方案包括 autoreleasepool 并在每个循环中创建一个新的 PDFDocument 对象并使用它。第二个选项确实释放了内存,但速度太慢了。
func loadPDFDocument(){
let documentURL = Bundle.main.url(forResource: "TestPDF", withExtension: "pdf")!
if let document = PDFDocument(url: documentURL) {
for page in 1...document.pageCount {
DispatchQueue.global().async {
let innerDocument = PDFDocument(url: documentURL)!
print(innerDocument.page(at: page)!.string!)
}
}
}
}
【问题讨论】:
-
你解决了吗???
-
@JimBak 没有。我在 Apple 开发者论坛上也没有任何回应。我已经开始研究我的应用程序的其他领域。当我在提交之前重新审视这个问题时,我很可能会使用我的开发人员会员资格的代码支持之一
-
感谢您告诉我们!
-
您是否使用过 Instruments 来查看问题所在?
标签: memory-management swift4 pdfkit