6

Does the recursion heavily impose on processor and ram? I mean, that one of my threads has a method, that is very likely to call itself. Let's say that It can self-call about one time per second. My app should run for at least 24 hours without stopping, so it gives (60*60*24) 86400 self-called methods.

How does it influence on the second (main) thread?

Sorry for my bad english, and for no code, but im not writing from home.

5
  • Once you get into that territory, you have to worry about having enough space on the stack. Commented Aug 7, 2012 at 20:20
  • 1
    Does it return from the recursive call? Commented Aug 7, 2012 at 20:21
  • @Marvo Its like: doSth -> call-self -> return Commented Aug 7, 2012 at 20:24
  • 1
    @kittyPL That doesn't necessarily mean that it won't go on forever. The "call self" step should be conditional, otherwise your function will continue calling itself indefinitely. Commented Aug 7, 2012 at 20:27
  • Its conditional, but the condition occurs once in day ;> Commented Aug 7, 2012 at 20:33

4 Answers 4

8

If there are no return statements which will end the string of recursive calls before the 86400th call, it is likely that you will have a stack overflow error from there being too many recursive calls on the stack. Try implementing an iterative solution if possible.

Sign up to request clarification or add additional context in comments.

1 Comment

first and best answer :) hope you earn much more reputation soon :D
4

Recursive calls are very inefficient in terms of memory.
This is because each recursive call adds a new frame to the stack and so for N calls you have O(N) memory requirements.
Recursive methods solve difficult problems in a really simple way (e.g. traversal of a tree) with simple code.
The downside is that if you don't know what you are doing you could get run out of memory as a result of too many recursive calls.
So if you know that a problem can be solved recursivelly but you need too much recursion try to implement it iteratively (most but not all recursive algorithms can be transformed to iterative ones)

Example. In my Windows 32-bit (4GB) the following gets a Exception in thread "main" java.lang.StackOverflowError after 7380 calls

public static void recursing( int n ){
        System.out.println(n++);
        recursing(n);
}  
public static void main(String[] args) {
    recursing(1);

}

3 Comments

Stack frames are pretty small (in the order of 8-32 bytes each), are generally cached and get released as soon as you return. I don't think it's fair to describe them as "very inefficient" - in fact they are one of the most efficient forms of memory available as long as you don't exhaust the available stack space.
I describe as inefficient the recursive calls.Not the memory as you say
You say "Recursive calls are very inefficient in terms of memory". Perhaps worth clarifying / saying what you really mean :-)
1

I'm not sure if it is appropriate to your problem, but it sounds like a scheduler maybe useful as you are basically saying that it should run once a second. You could try using the Quartz Scheduler.

You can create a Job and then by using a simple trigger or cron trigger tell it to run every second forever. Documentation for Quartz.

Comments

1

In Java, using a loop is often more efficient than using recursion. There are some cases where recursion is the most efficient.

One processor can easily make 10 million calls per second or trillions per day. 86400 isn't that much and I wouldn't worry about.

Let's say that It can self-call about one time per second.

That doesn't make much sense. Using a loop is a much approach. Only use recursion if you intend to return when finished.

2 Comments

Well, in 7380 calls I get a Exception in thread "main" java.lang.StackOverflowError in the example I put in my answer.Unless you are talking about something else
IMHO any more than 20 is pretty long. :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.