gc() which is known to increase free memory. You can keep checking this after a memory gobbling code executes. Easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options -Xmx512M , this will immediately solve your OutOfMemoryError.