Because I was having problems with stack overflowing due too deep recursion, I've tried playing around it using while loop. but even tho exiting the recursion before the overflow and re-entering the recursion still causes overflow, how do I clear the allocated memory of the stack that contains the recursion after I exit it?
I'm not sure about that .. but you can try - System.runFinalization(); OR System.runFinalizersOnExit();
When your application close the stack should be cleared your recursion is still probably going too deep. Check your code carefully when you are using recursivity make sure your recursive method end. If your recursive method does end, you could increase your stack size but I would not recommend this. Instead, you should ask your self if your method.
but if you want to increase the stack size check this link: How to increase the Java stack size?