Under the documentation for OutOfMemoryError it declares that it will not be thrown unless the VM has failed to reclaim memory following a full garbage collection. So if you keep allocating memory until you get the error, you will have already forced a full garbage collection.
Presumably the question you really wanted to ask was “how can I reclaim the memory I think I should be reclaiming by garbage collection?”
—
To manually Request GC (not from System.gc()) :
Go To : bin folder in JDK eg.-C:Program FilesJavajdk1.6.0_31bin
Open jconsole.exe
Connect to the desired local Process.
Go To memory tab and click perform GC.