I am not pro in Java but your problem can be solved by "blockingqueue" if you use it wisely.
Try to retrieve a chunk of records first, process them, and iterate the process until you complete your processing. This may help you to get rid of the OutOfMemory Exceptions
.