That is a very generalized question. Actually you get outofmemory error when jvm does not have enough memory on the heap for creating new objects.
Generally the default heapsize can be altered by two VM arguments
-Xms256m (for initial heap size) and -Xmx512m (for max. heap size) to allocate the heap size to be allocated to your
java application. The arguments to the VM above depend upon the actual memory you have.
The above optimization works assuming that you don't have any memory leaks in your application code. If you still find such problems i suggest that you use an appropriate java memory profiler to identify any memory leaks.
I suggest that you have a look at the java performance forum also.