OutOfMemoryError Exception in ArrayList

Hi All!

I do not know where to post this since I am having an OutOfMemoryError when I try to place a query of 100,000 and more records to an ArrayList.

I got the error message like:

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 MyGrid.refreshGrid(MyGrid.java:79)

at MyGrid.(init)(MyGrid.java:54)

at MyGrid.main(MyGrid.java:117)

Press any key to continue...

This error message resulted when I run a query that will return 167645 rows and add those data to an ArrayList and place the array list to a JTable object. But if I run a query that only returns 163 rows, the program will not return any exception.

I just want to try displaying the records returned from a query. I have tried the ensureCapacity method of the ArrayList and set it to 1,000,000 but still there is an OutOfMemoryError Exception.

I am not really familiar with all of these since I am just new with java and is using VB as my development language.

I needed a component that is similar to a datagrid or flexgrid control in visual basic that could hold millions of records.

Below is a simple code for excuting the query and then store the returned records and data to an array list and set the array list to a table.

Code for reloading table data from query

public void refreshGrid(JTable table, String query, String[] columnHeaders){

ArrayList values=new ArrayList();

values.ensureCapacity(100000000);

try{

rs=stmt.executeQuery(query);

while(rs.next()){

for (int x=1; x<=columnHeaders.length;x++){

values.add(rs.getString(x));

}

}

System.out.println("size of array: "+values.size());

int noOfRows=(values.size())/columnHeaders.length;

int no=0;

Object[][] data=new Object[noOfRows][columnHeaders.length];

System.out.println("no of records : "+noOfRows);

for(int i=0;i<noOfRows;i++){

for(int j=0;j<columnHeaders.length;j++){

String s=(String)values.get(no++);

data[j]=s;

}

}

dtm.setDataVector(data, colHeads);

table.setModel(dtm);

}catch(SQLException e){

System.out.println(e);

}

//Closing the connection.

finally{

try{

if(con!=null){

con.close();

}

}catch(SQLException e) {

System.out.println(e);

}

}

}

this code works fine if there are only hundreds of records returned from the query but if it is thousands or more... the error occurs.

Please help me how to solve this one... I really do not have any Idea how to deal with this..>

[2921 byte] By [khickyphutza] at [2007-11-26 16:45:46]
# 1

Heap space errors are thrown when your java VM run out of memory. It indicates you are making too many objects. You can set the amount of memory the VM use at startup as a commanline parameter; i.e. java -Xms512M will allocate maximum 512 Megabytes of memory for the VM to use. Default is 56M? See startup parameters at http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/java.html#options

You can of cource change your program to not using so much memory by maybe load objects on demand or some other appropriate way for you implementation. But allocating more memory is easier at least :)

Regards

Fluid

Fluida at 2007-7-8 23:13:06 > top of Java-index,Java Essentials,Java Programming...
# 2
:(Cross posted: http://forum.java.sun.com/thread.jspa?threadID=5130745
macrules2a at 2007-7-8 23:13:06 > top of Java-index,Java Essentials,Java Programming...