简体   繁体   中英

Exception java.lang.OutOfMemoryError: Java heap space

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM