我正在使用Java Print Service打印tiff文件,它会导致“ java.lang.OutOfMemoryError:Java堆空间”。
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);
}
}
堆栈跟踪:
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.<init>(Unknown Source)
at java.awt.image.Raster.createPackedRaster(Unknown Source)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
at java.awt.image.BufferedImage.<init>(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)
tif文件非常小,超过500K,包含7页。 Google搜索了很多,但是找不到太多帮助。看到一个帖子,建议在打印之前明确调用垃圾回收。试过了,但是没有用。 JVM堆大小已设置为最大512mb。
java-vm-args="-Xms128m -Xmx512m -XX:PermSize=32m -XX:MaxPermSize=128m"
该程序的目标是在处理完货件后自动将证书的副本自动打印(无需用户交互)到用户的默认打印机。
由于文件不那么大,为什么会引起内存问题,以及如何解决?
谢谢。
答案 0 :(得分:0)
请尝试以下步骤。
使用以下命令从命令提示符启动应用程序
java -jar -Xmx1024M -XX:+ PrintGCDetails -XX:+ PrintGCDateStamps“ \ your.jar” >> C:\ Temp \ Log.txt
打开一个新的命令提示符,并导航到JDK Bin目录并键入
jcmd
现在记下您的进程的进程ID并输入
jmap -heap
这将帮助您弄清楚应用程序正在发生什么。请尝试并告诉我们是否有帮助。