The following are some possible "band-aid" solutions. The real solution is to do a proper investigation of the cause of your excessive memory usage, and develop a considered fix for it.
Bandaid solution #1: Increase the heap size. Keep increasing the heap size until the OOMEs stop.
Bandaid solution #2: If you call query.setMaxResults(count)
before calling list()
you should get at most count
elements in the list. If count
is small enough you should get no OOMEs.
Bandaid solution #3: Don't retrieve the values to a list. Instead, do something like this instead:
Iterator<Object> iter = query.iterate();
while (iter.hasNext()) {
Object row = iter.next();
}
Note that the OOMEs could actually be a symptom of a (different) memory leak. None of the above band-aid solutions will work for that ... in the long term.