Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

db , java , mysql India
  • 13 years ago
    Hi friends .

    I was executing a query on large data in database. then i found java heap space error and it could not execute query.
    i'm giving the code below..
    I want to execute the query in batch to select the data..if suggest if there is any other way.

    ResultSet rs=null;
            Connection conn = null;
            Statement stmt = null;
                     System.out.println("Database Driver loading .");
                     System.out.println("Connecting to server...");
                        conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","user","pwd");
                     catch(Exception e)
                        System.out.println("Database connection failed !");
                     System.out.println("Server connected.");
                     stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE,
                      //int fetchSize = stmt.getFetchSize();
                     System.out.println("Executing query.....");
                     rs=stmt.executeQuery("Select * from table");

                 catch(SQLServerException SQLEx)
                  catch(Exception Ex)
                 System.out.println("Query Execute Sucessfully...");

    the error was

    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
            at java.util.Arrays.copyOf(Arrays.java:2760)
            at java.util.Arrays.copyOf(Arrays.java:2734)
            at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
            at java.util.ArrayList.add(ArrayList.java:351)
            at com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:2306)
            at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:423)
            at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:1991)
            at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:1413)
            at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1757)
            at com.mysql.jdbc.Connection.execSQL(Connection.java:2998)
            at com.mysql.jdbc.Connection.execSQL(Connection.java:2927)
            at com.mysql.jdbc.Statement.executeQuery(Statement.java:956)

    I need help to solve my query execution problem on large data.
    I'm working on Windows. I set the ANT_OPTS=-Xmx512M in System Variable.

  • 13 years ago
  • 12 years ago


    it was really very helping one

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”