Variables passed as arguments to a method are passed on the current thread’s stack. Method variables of primitive types are also allocated on the stack. This is referred to as a “stack frame”, and is automatically collected when the method exits. When you call a method from within a method, you are creating an additional stack frame, and the size of this frame depends on the number and type of arguments and local variables in the called method.
Recursive algorithms are algorithms where a method calls itself in a loop to complete a task. As a result, they create multiple stack frames. Although they can be very elegant algorithms, in mobile Java they have two major strikes against them as listed:
They use a lot of stack memory. The same method is called repeatedly, and only when the application completes does it unwind the queued stack frames. This extra stack memory is often not useful, and stack memory per thread is limited and such heavy stack use may cause an OutOfMemoryException well before you are actually out of heap memory.
Recursive algorithms can be slow. Each method call includes a certain amount of overhead, which is not really necessary since a recursive algorithm can be unwound into a non-recursive equivalent loop that does not include the relatively heavy method call.
In general, you should avoid recursive algorithms in mobile Java and be sure to minimise both the number of method calls for trivial things and the number of arguments passed in as arguments to each method. You may be able to shift arguments to be constants, or pass several arguments together as a single object to reduce stack overhead.