setMaxResult takes as argument the amount of data you want to fetch, so instead of 50000 it should be 20. Also make sure to do flushing and clearing the session after some interval to free the space.
For Example:
Code:
int pageSize = 20; // how many objects to retrieve in one query
int numRows = 50 000; // get this via a count() query
for (int i=0; i < numRows/pageSize; i++) {
// query such that everything you need is retrieved in one query
Query q = sess.createQuery("QueryString");
q.setFirstResult(i*pageSize);
q.setMaxResults(pageSize);
List data = q.list();
// Iterate over data and do something, then evict objects and can also call session.clear and session.flush
}
Rating appreciated if it helped.