Use -Xmx to set the maximum heap size (which may use hard drive space as Java heap). e.g. set the max heap size to 10GB:
java -Xmx10g MainClass
It may be further optimized using garbage collectors such ConcurrentMarkSweep:
java -Xmx10g -XX:+UseConcMarkSweepGC MainClass