aspose file tools*
The moose likes Performance and the fly likes java.lang.OutOfMemoryError: Java heap space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "java.lang.OutOfMemoryError: Java heap space" Watch "java.lang.OutOfMemoryError: Java heap space" New topic
Author

java.lang.OutOfMemoryError: Java heap space

Pankaj Chaudhary
Greenhorn

Joined: Jun 05, 2006
Posts: 16
I have problem when i have to print 1500 pdf file concurently.the message shown on browser is "java.lang.OutOfMemoryError: Java heap space".this message is shown after 100 printing of pdf file.Plz help me in this topic
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Exactly how are you printing pdf files from Java? PDF files on the browser side are handled by the Adobe plugin. Are the PDF files being sent to a browser from a Java servlet?
Bill
Pankaj Chaudhary
Greenhorn

Joined: Jun 05, 2006
Posts: 16
hiii ,
i m using following code in action servlet for making pdf

File xsltfile = new File("../webapps"+path+"/resources/xsls/admin/totalpayout.xsl");
File pdffile = new File("../webapps"+path+"/resources/pdfs/admin/totalpayout.pdf");
org.apache.fop.apps.Driver driver = new org.apache.fop.apps.Driver();

Logger logger=new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
driver.setLogger(logger);
MessageHandler.setScreenLogger(logger);

driver.setRenderer(org.apache.fop.apps.Driver.RENDER_PDF);
OutputStream out = new java.io.FileOutputStream(pdffile);
driver.setOutputStream(out);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));

Source src = new StreamSource("../webapps"+path+"/resources/xmls/admin/totalpayout.xml");

//Resulting SAX events (the generated FO) must be piped through to FOP
Result result = new SAXResult(driver.getContentHandler());
transformer.transform(src, result);
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
So you are really talking about generating PDF files in a servlet, not "printing."

Generating PDF just naturally takes a lot of memory since FOP has to create an entire representation of each document in memory.
You should approach this problem by either:
1. Giving the servlet engine enough memory
-or-
2. Restricting the number of simultaneous generation threads running.
-or-
a combination of both.
Also, be sure that your code does not hold on to references to objects after the PDF has been generated.
Bill
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42930
    
  68
So you are really talking about generating PDF files in a servlet, not "printing."


And I was already imagining how to print 1500 copies of anything concurrently - that would need to be quite a printer farm!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
And indeed, generating 1500 different PDFs concurrently still sounds excessive. To expand on Bill's "2. Restricting the number of simultaneous generation threads running" - I would consider using a thread pool, with a max size that you can configure. Thus you can limit the number of jobs which can truly proceed concurrently, which allows you to avoid the OutOfMemoryError and may also improve performance. Even if memory were infinite, the more simultaneous threads you have going, the more time is spent simply switching from one thread to another. You reach a point of dimiinishing returns where it becomes more efficient to finish some of the jobs you have already, before adding new threads.

Note that in JDK 5 and beyond, you can use ThreadPoolExecutor and related classes to manage a thread pool with a minimum of hassle. For earlier JDK versions, there are various open-source implementations of thread pooling available.
[ June 19, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Pankaj Chaudhary
Greenhorn

Joined: Jun 05, 2006
Posts: 16
thanks all of u i got solution now bye increasing memory size using environment variable java_opts
Manoj Raghuwanshi
Ranch Hand

Joined: Jun 20, 2004
Posts: 75
When I saw Pankaj´┐Żs post first , I wonder how he can able to print PDF from java.Because I spent 1 month on searching how to print pdf from java and then came to conclusion that it is not possible without taking help of a third party library like Icepdf.
Any one knows whether Sun has added support for printing pdf files in new java versions?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.OutOfMemoryError: Java heap space