Note: this should not prevent the OutOfMemoryError but you need to learn about the JVM memory arguments, because it should help for any case.
You need to take a look at the JVM memory parameters. actually you can set the as much memory as you want to your JVM :
-Xmx2048m -> this param to set the max memory that the JVM can allocate
-Xms1024m -> the init memory that JVM will allocate on the start up
-XX:MaxPermSize=512M -> this for the max Permanent Generation memory
and you may want to check this parameters also.
-XX:MaxNewSize= -> this could be 40% from your Xmx value
-XX:NewSize=614m -> this could be 40% from your Xmx value
also you may tell you JVM what type of GC to use like (i think its already use by default in the earlier versions)
-XX:+UseConcMarkSweepGC