Memory leak iterating collections
Hello,
I have a UXP script that iterates over all TextStyleRanges of all Paragraphs of all PageItems of all Pages of a document, and it seems to be leaking memory.
My document has 140 pages, each page has 1-3 page items, each having 15-20 paragraphs with a few TextStyle ranges each. At about 20 pages, memory consumption is already 4GB and it quickly grows to 100%, at which point the entire process slows down so much that it takes hours to process the entire document.
I feel like the collections provide only a facade to InDesign object, which are only created as needed, except once they are created they remain in memory.
Is there any way to release a collection to reclaim the memory? Or any other workarounds?
For reference, here is how I am iterating over my document:
const pages = app.activeDocument.pages.everyItem().getElements();
for(let iPage = 0; iPage < 20; iPage++){
const page = pages[iPage];
const pageItems = page.allPageItems;
for(let iPageItem = 0; iPageItem < pageItems.length; iPageItem++){
const pageItem = pageItems[iPageItem];
if (!pageItem.paragraphs) {
continue;
}
const paragraphs = pageItem.paragraphs.everyItem().getElements();
for (let iParagraph = 0; iParagraph < paragraphs.length; iParagraph++){
const paragraph = paragraphs[iParagraph];
const ranges = paragraph.textStyleRanges.everyItem().getElements();
for (let iRange = 0; iRange < ranges.length-1; iRange ++){
const range = ranges[iRange];
}
}
}
}Any help is much appreciated 🙂
David
