title: iText memory management - PdfReader/Watermarking loads too much
body: <p>I'm watermarking documents, and I don't want to have to load them completely to memory, as they can be quite large. I found that RandomAccessFileOrArray that kind of buffers the reading, which it does fine but still loads too much to my liking.</p>
  
  <p>That is, after I load a 5 Mb PDF file, the used memory increases 23Mb ! And when I start watermarking it it jumps another 27Mb ! After that used memory gradually increases, but not horribly.</p>
  
  <p>Is there a reason to such behaviour ? Would you know a way to define the buffer size of the PdfReader or RandomAccessFileOrArray or something else ?</p>
  
  <p>Thanks for your input.</p>
  
  <hr>
  
  <p>The method printMem shows the status of the memory by showing free - used - total.</p>
  
  <p>Here is my code</p>
  
  <pre><code>printMem("Before load");
      PdfReader reader = null;
      try {
          reader = new PdfReader(new RandomAccessFileOrArray(new FileInputStream("C:/TEMP/zip/100258.pdf")),null);
          printMem("After load");
          FileOutputStream out = new FileOutputStream(f);
          PdfStamper stamp = new PdfStamper(reader, out);
  
          int numPages = reader.getNumberOfPages();
          int page=1;
          BaseFont baseFont = 
              BaseFont.createFont(BaseFont.HELVETICA_BOLDOBLIQUE,
                  BaseFont.WINANSI, BaseFont.EMBEDDED);
          float width;
          float height;
  
          while (page &lt;= numPages) {
              printMem("Page " + page);
              PdfContentByte cb = stamp.getOverContent(page);
              height = reader.getPageSizeWithRotation(page).getHeight() / 2;
              width = reader.getPageSizeWithRotation(page).getWidth() / 2;
  
              cb.saveState();
              cb.setColorFill(MEDIUM_GRAY);
  
              // Primary Text
              cb.beginText();
              cb.setFontAndSize(baseFont, PRIMARY_FONT_SIZE);
              cb.showTextAligned(Element.ALIGN_CENTER, "WatermarkText", width,
                      height, TEXT_TILT_ANGLE);
              cb.endText();
  
              cb.restoreState();
              page++;
          }
          stamp.close();
      } catch(Throwable e) {
          reader = null;
          System.gc();
      }
  </code></pre>
  
  <p>And here is the partial output:</p>
  
  <pre><code>Before load | 1566248160 6615840 1572864000
  After load | 1542392472 30471528 1572864000
  Page 1 | 1515096880 57767120 1572864000
  Page 2 | 1515095992 57768008 1572864000
  Page 47 | 1512998840 59865160 1572864000
  Page 48 | 1512998840 59865160 1572864000
  </code></pre>