I am trying to merge 40 1GB files into 1 40GB file by doing roughly the following:
List<string> files; // has all 40 filenames
Document mergeDoc = new Document();
// other setup
try
{
foreach(string file in files)
{
Document srcFile;
try
{
srcFile = new Document(file);
mergeDoc.Pages.Add(srcFile.Pages);
// a lot of other processing, including specific bookmark processing
}
catch { // error handling }
finally
{
srcFile.Dispose();
}
}
}
finally
{
mergeDoc.Save();
mergeDoc.Dispose();
}
I have a few questions:
Q1) What would be the peak amount of RAM usage with this approach? I estimate it would be around 41GB (40GB for mergeDoc + 1GB for the final srcFile).
Q2) Working off of Q1, this program must be able to handle 5 threads of the above code at the same time. If each thread has 40GB total of files to merge would the system require 200GB+ of RAM?
Q3) Would opening, saving, and closing mergeDoc inside of the foreach loop provide any memory savings?
Q4) Is there a way to append pages, and cherry-pick bookmarks, from a srcFile to mergeDoc without having the entirety of mergeDoc loaded into memory?
Q5) Is there another way to process these merges in a more memory efficient manner that I haven’t mentioned?
Note: I do not think I can utilize PdfFileEditor.Concatenate() as the program needs to process bookmarks and handle merge errors file-by-file, not post-merge.