Have you tried using a step through debugger to see how far you get before the out of memory error? If you dont know how to use a step through debugger, toss a couple of these in your perl code.
system("read -p \"Press any key\"");#unix
#or
system("pause");#windows