2

I have been wondering: Why doesn't the paint() method generate a StackOverflowError? I know that paint() must run repeatedly in the background, but what restricts it to only calling it at a speed which the computer can handle without generating the StackOverflowError? So that it doesn't repeat paint() to the point of using too much memory?

DBedrenko
  • 4,871
  • 4
  • 38
  • 73
tasteslikejava
  • 80
  • 2
  • 10
  • 1
    Which code in the implementation of `paint` do you see as calling `paint` again? – Marko Topolnik Jan 15 '14 at 13:35
  • 1
    A stack overflow is (almost always) caused by recersive functions that goes too deep. Calling something over and over will not cause a StackOverflow (but may use up all available CPU; slowing other applications) – Richard Tingle Jan 15 '14 at 13:37
  • 1
    Do you know what is a `StackOverflowError` and what causes it? This has nothing to do with the `paint()` method, unless you override it making an infinite recursive call. – BackSlash Jan 15 '14 at 13:38
  • A poor recursive call would be the most common reason for a StackOverflow error. Look at this to find out more about StackOverflow and its probable causes. http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error – Hrishikesh Jan 15 '14 at 13:39

1 Answers1

6

Stack Overflow occurs only with recursive function

void paint() {
    // code ...
    paint();
}

Stack:

-> stack overflow <-
paint()
...
paint()
paint()
paint()
...
main()

But in your case the following is happening:

while (true) {
    paint();
}

paint() is being called always from the same stack frame

ichramm
  • 6,437
  • 19
  • 30