2

IDE Used:Netbeans 8.1.

Reading large data from MySQL database. Below is my code:

List outer=new ArrayList<String>();
List inner=new ArrayList<String>();
Connection con;
Statement stmt;
ResultSet rs;
ResultSetMetaData rsmd;
int columnNumber;
try{
    Class.forName("com.mysql.jdbc.Driver");
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");
    stmt=con.createStatement();
    rs=stmt.executeQuery("select * from mytable where srno<1000");
    rsmd=rs.getMetaData();
    columnNumber=rsmd.getColumnCount();
    while(rs.next()){
        for(int i=1;i<columnNumber;i++){
            inner.add(rs.getString(i));
        }
        outer.add(inner);
    }           
    System.out.println("\t" + outer);
    rs.close();
    con.close();
}catch(Exception e){
    System.out.println(e);
}

I am getting error while running the code: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I've tried to set vm options in properties>run>VM Options: -Xmx1024m

I am still getting the same error.How do I resolve it?

6
  • Query a smaller table or use a WHERE clause to limit how much data comes back. Run 64 bit JVM with lots more RAM. That's all you can do. Commented Aug 10, 2016 at 9:20
  • Did it. But still getting the same error. Commented Aug 10, 2016 at 9:22
  • Give more details about select result: how many rows? how many columns? average size of each column content? Commented Aug 10, 2016 at 9:24
  • There are total 10,67,000 rows and 45 columns. Commented Aug 10, 2016 at 9:28
  • 1
    Suppose each column has an average size of x bytes you finish with x*43MB. If the average size of each column is 10 bytes you have 0,5GB in the heap. No way. Commented Aug 10, 2016 at 10:13

1 Answer 1

4

Just look at this code:

   while(rs.next()){
     for(int i=1;i<columnNumber;i++){
         inner.add(rs.getString(i));
     }
     outer.add(inner);
   }   

For each row in the table, you are adding each column value into a list and each of those lists you are adding to another list. So if your table has 1 million rows and each row has 5 columns. You are creating 5 million rows. is it a surprise that memory is being exhausted? According to your update it's actually 10 Million rows and 45 columns. Do you realize that you are making 450 million objects!

Also remember that each item on the outer list will take more space than the amount of space those columns took on the database because of the object headers and other stuff.

You have explained what this is for, if it's for a swing app for example, you can choose a suitabl TableModel and avoid loading the whole db into memory at once.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh my mistake. I wanted to store whole row in a list and there are multiple rows, that's why did it this way.
A completely different strategy is needed.
I agree with @e4c5. You need to use data while you're reading from db (lazy list or paginated list). It's never a good strategy to load in memory data from a potential unlimited data source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.