0

I am running out of memory when I am trying to read a huge list. This is the code that crashes the server

query = "SELECT * FROM huge_list ORDER BY id ASC";
statement = this.database.createStatement();
results = statement.executeQuery(query);

This is the error

Exception in thread "Thread-2" java.lang.OutOfMemoryError: Java heap space

the error points to the results line. Any advice on how to avoid that? Perhaps load the list using Limit? Could the size of the list cause an error?

user2253741
  • 165
  • 6

3 Answers3

1

You could may change the memory settings of your jvm, this could fix your problem but in general you should not load all data at once. Better load always let's say 100 rows and query then the next 100 rows.

Just for security I would store the smallest ID and query that again with limit the next brunch of rows. So this will not break your logic if new rows are added.

rekire
  • 47,260
  • 30
  • 167
  • 264
0

I believe you should be streaming the MySQL results. See Reading large amount of records MySQL into Java

Your other option would be to increase the maximum RAM allowed by the JVM, but that's probably not what you want to do.

Community
  • 1
  • 1
0

Try to modify the query to return only those records which you are displaying/processing to the user. You can use limit by clause. One more thing state your column names, SELECT * isn't really needed, are you really processing all the cols that your query returns?

sol4me
  • 15,233
  • 5
  • 34
  • 34