title: java.lang.OutOfMemoryError when printing tiff file using Java Print Service
body: <p>I'm using Java Print Service to print a tiff file, it causes "java.lang.OutOfMemoryError: Java heap space".</p>
  
  <pre><code>      private void sendToPrinter(String fileToPrint) throws PrintException, IOException
        {
            /* Use the pre-defined flavor for a GIF from an InputStream */
            DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
  
            /* Create a set which specifies how the job is to be printed */
            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            aset.add(MediaSizeName.NA_LETTER);
            aset.add(new Copies(1));
  
            /* Create a Print Service using the default printer */
            PrintService ps=PrintServiceLookup.lookupDefaultPrintService();
            DocPrintJob job=ps.createPrintJob();
  
  //          System.gc();
            try (FileInputStream fis=new FileInputStream(fileToPrint))
            {
                Doc doc=new SimpleDoc(fis, flavor, null); 
                job.print(doc, aset);
            }
        }
  </code></pre>
  
  <p>Stack trace:</p>
  
  <pre><code>2020-04-23 15:32:03|DEBUG|SHPSKD010_160_TEST|Printing \\kanesrv02\imagedocs\certs\61107282.tif
  2020-04-23 15:32:20|ERROR|SHPSKD010_160_TEST|java.lang.OutOfMemoryError: Java heap space
      at java.awt.image.DataBufferInt.&lt;init&gt;(Unknown Source)
      at java.awt.image.Raster.createPackedRaster(Unknown Source)
      at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
      at java.awt.image.BufferedImage.&lt;init&gt;(Unknown Source)
      at sun.java2d.loops.GraphicsPrimitive.convertFrom(Unknown Source)
      at sun.java2d.loops.GraphicsPrimitive.convertFrom(Unknown Source)
      at sun.java2d.loops.MaskBlit$General.MaskBlit(Unknown Source)
      at sun.java2d.loops.Blit$GeneralMaskBlit.Blit(Unknown Source)
      at sun.java2d.pipe.DrawImage.renderImageXform(Unknown Source)
      at sun.java2d.pipe.DrawImage.transformImage(Unknown Source)
      at sun.java2d.pipe.DrawImage.scaleImage(Unknown Source)
      at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
      at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
      at sun.java2d.pipe.ValidatePipe.copyImage(Unknown Source)
      at sun.java2d.SunGraphics2D.copyImage(Unknown Source)
      at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
      at sun.awt.windows.WPathGraphics.drawImageToPlatform(Unknown Source)
      at sun.print.PathGraphics.drawImage(Unknown Source)
      at sun.print.PathGraphics.drawImage(Unknown Source)
      at sun.print.ImagePrinter.print(Unknown Source)
      at sun.print.RasterPrinterJob.printPage(Unknown Source)
      at sun.print.RasterPrinterJob.print(Unknown Source)
      at sun.print.Win32PrintJob.printableJob(Unknown Source)
      at sun.print.Win32PrintJob.print(Unknown Source)
      at com.kanebridge.op.ui.SHPSKD010_160_TEST.sendToPrinter(Unknown Source)
      at com.kanebridge.op.ui.SHPSKD010_160_TEST.PRTALL_actionPerformed(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at com.kanebridge.common.ui.BasePanel.callMethod(Unknown Source)
      at com.kanebridge.common.ui.BasePanel.callMethod(Unknown Source)
  </code></pre>
  
  <p>The tif file is pretty small, a little over 500K, containing 7 pages.
  Googled quite a bit, but couldn't find much help. Saw a post suggesting calling the Garbage Collection explicitly before printing. Tried, but did not work.
  JVM heap size already set to max of 512mb.</p>
  
  <p><code>java-vm-args="-Xms128m -Xmx512m -XX:PermSize=32m -XX:MaxPermSize=128m"</code></p>
  
  <p>The goal of the program is to print a copy of the certificate automatically (without the user interaction) to user's default printer after finishing processing a shipment.</p>
  
  <p>Why would it cause the memory issue since the file is not that large, and how to resolve?</p>
  
  <p>Thanks in advance.</p>