First of all, use a memory profiler such as YourKit
to figure out what it is exactly that’s consuming the memory (for example, it could be due to the accidental retention of some unneeded references). Once you understand how your program is actually using the memory, you can formulate a plan of attack.
solved out of memory for Java application