Each time you do a recursive call, the call takes up some memory from the stack. The size of the stack is limited, it doesn't grow automatically, so it doesn't really matter that your PC has 3 GB RAM.
If you would enter 1,000,000,000 as the input, you'll make a billion nested method calls. I don't know exactly how many bytes a stack frame for a method call costs, but it's probably a few tens to a few hundred bytes. So if you do a billion nested calls, this will cost tens to hundreds of gigabytes of memory! That will never fit.
You should rewrite your method so that it is not recursive if you want this. And even then I doubt if it is going to work, because the sequence of factorials grows very fast. A number such as 1000000000! is so big that it might be hard to fit it in memory by itself.
This will set the stack size to 2 MB. However, as I already said, for 1 billion nested method calls, you'd need to set it to 10 GB or 100 GB. That's not going to work - your computer doesn't have that much memory. Besides that, the operating system might have a limit for the stack size of threads, which is most likely far less than a gigabyte.
Joined: May 13, 2010
ok its not completely impossible..
so therotically if i have memory more than 10 GB on in TB i can run such recursive function.
now question is if i cant run my current program for large input, what modification i need to do in my current code to make it work.?