1

My java app fetches about 200,000 records in its result set.

While trying to fetch the data from Oracle DB, the server throws java.lang.OutOfMemoryError: Java heap space

One way to solve this, IMO, is to fetch the records from the DB in smaller chunks (say 100,000 records in each fetch or even smaller count). How can I do this (meaning what API method to use)?

Kindly suggest how to do this or if you think there's a better way to overcome this memory space problem, do suggest. I do not want to use JVM params like -Xmx because I read that that's not a good way to handle OutOfMemory errors.

3
  • setFetchSize method should help. A smarter WHERE clause and processing strategy might be good, too. Where'd you read that? It's only bad if you don't understand root cause and postpone the inevitable. Commented Nov 23, 2013 at 15:48
  • 1
    What is a "lakh record"? Commented Jan 18, 2014 at 10:23
  • 1
    lakh means 100,000. For the benefit of the non-Indian readers here though it should probably be written "100,000" :) Commented Jan 18, 2014 at 11:05

1 Answer 1

1

If you are using Oracle DB you may set AND ROWNUM < XXX to your SQL query. This will cause that only XXX-1 rows will be fetched in query.

Other way is to call statement.setFetchSize(xxx) method before executing statement.

Setting larger JVM memory pool is poor idea, because in future there may be larger data set which will cause OOM.

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

Comments

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.