Link to home
Start Free TrialLog in
Avatar of thecaz
thecaz

asked on

java.lang.OutOfMemoryError (creating Excel File with HSSF)

Hi all.
I'm trying to solve a outofmemory problem with a servlet creating a Excel file.
I've downloaded TOMCAT 5 and set
CATALINA_OPTS = -server -Xmx1200M -Xms1200M -Xss256k

Here there is the message "
"exception
 javax.servlet.ServletException: Servlet execution threw an exception
 root cause
 java.lang.OutOfMemoryError"
At the end there is a sample servlet giving this error (Note that reducing the for cycle the error disappear! and this is normal!).

Anyone could solve this problem?

Thanks!!!!!!!!!!!!!!

Thecaz








Here there is teh Servlet

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.poi.hssf.usermodel.*;

public class PrimoServlet extends HttpServlet {
    public void init(ServletConfig config) throws ServletException {
        super.init(config);  
    }

    public void destroy() {
    }

    /** Processes requests for both HTTP GET and POST methods.
     * @param request servlet request
     * @param response servlet response
     */

    protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/vnd.ms-excel");
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");

       
        for (int i=0; i<10000; i++) {
           // Create a row and put some cells in it. Rows are 0 based.
           HSSFRow row     = sheet.createRow((short)i);

           // Create a cell and put a value in it.
           HSSFCell cell   = row.createCell((short)0);

           cell.setCellValue(1);

           // Or do it on one line.
           row.createCell((short)1).setCellValue(1.2);
           row.createCell((short)2).setCellValue("This is a string");
           row.createCell((short)3).setCellValue(true);
           row.createCell((short)4).setCellValue("This is a string1");
           row.createCell((short)5).setCellValue("This is a string2");
           row.createCell((short)6).setCellValue("This is a string1");
           row.createCell((short)7).setCellValue("This is a string2");
           row.createCell((short)8).setCellValue("This is a string1");
           row.createCell((short)9).setCellValue("This is a string2");
           row.createCell((short)10).setCellValue("This is a string1");
           row.createCell((short)11).setCellValue("This is a string2");
           row.createCell((short)12).setCellValue("This is a string1");
           row.createCell((short)13).setCellValue("This is a string2");
           row.createCell((short)14).setCellValue("This is a string1");
           row.createCell((short)15).setCellValue("This is a string2");
           row.createCell((short)16).setCellValue("This is a string1");
           row.createCell((short)17).setCellValue("This is a string2");
           row.createCell((short)20).setCellValue("This is a string1");
           row.createCell((short)21).setCellValue("This is a string2");
           row.createCell((short)22).setCellValue("This is a string1");
           row.createCell((short)23).setCellValue("This is a string2");
           row.createCell((short)24).setCellValue("This is a string1");
           row.createCell((short)25).setCellValue("This is a string2");
           row.createCell((short)26).setCellValue("This is a string1");
           row.createCell((short)27).setCellValue("This is a string2");
       }
         // Write the output
        OutputStream out = response.getOutputStream();
        wb.write(out);
        out.close();
    }

    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */

    protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Handles the HTTP POST method.
     * @param request servlet request
     * @param response servlet response
     */

    protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    /** Returns a short description of the servlet.
     */

    public String getServletInfo() {
       return "Example to create a workbook in a servlet using HSSF";
    }
}
ASKER CERTIFIED SOLUTION
Avatar of pat5star
pat5star

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thecaz
thecaz

ASKER

Hi Pat.
I know that 1200 is a strange value.

I forgot to say that I'm using tomcat under Windows XP and I lunch Tomcat using the shortcut (I heard that It could be a problem, I would like to lunch it manually but i'm not able)


I tried to modify catalina.bat putting on the head

CATALINA_OPTS="-Xms64m -Xmx384m"

It worked just 2 or 3 times and then nothing.

I trie to put the variable using Control-Panel -> System ecc ecc ..  but nothing changed

Any idea?

Thecaz
I'm not sure how to do it with Windows, but this might work:

Right click on the icon that launches Tomcat. If you left click on properties and then click the shortcut tab you'll find an input box called target. You can specify options for java there. If that doesn't work I'm not sure how you would do it and hopefully someone else will be able to guide you.

-Pat
With POI, I think you need to build the whole thing in memory and then write it out. Thus, if you really do need 10,000 rows in the Excel table (!), then Tomcat will need the memory to store 10,000 rows. Following the trail pat5star started look promising.
Avatar of thecaz

ASKER

Hi all.
nggreer you are right. The first approach is impossible.

Now i'm trying mofifying the java opts and it seems working. I don't understand why JAVA_OPTS or CATALINA_OPTS doesn't work.
Bah!

TheCaz
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial