recursion and memory

前端 未结 5 1002
生来不讨喜
生来不讨喜 2020-12-30 11:19

I have a program that passes in huge amounts of data, say 1000 variables, through recursion. The recursion would run to atleast 50 or 60 times. What I\'m worried about is, i

相关标签:
5条回答
  • 2020-12-30 11:53

    There could be two cases

    1. Enough memory, recursion completes and you get ressult.

    2. Not mough memory amd get StackOverFlowError and program exits

    You will not have wrong results because of memory overwriting that does not happen

    0 讨论(0)
  • 2020-12-30 12:01

    There's no chance of getting wrong results: in case of a stackoverflow your program will terminate prematurely with a StackOverflowError.

    The memory locations where you store the data cannot be overwritten by anything else.

    0 讨论(0)
  • 2020-12-30 12:02

    Java or even for that matter C, your program memory will never change the state of unrelated memory.

    Though JVM doesn't support tail recursion. So at max you shall get StackOverFlowError when there is no more space available. You shouldn't worry about data corruption, but rather see if the recursion stack is just too high (say above 2000). Work on it if so

    0 讨论(0)
  • 2020-12-30 12:06

    is there a possibility of data getting over- written on the memory locations because there isn't much space

    Java stores objects in heap space and those values are only reclaimed by the garbage collector. That means that what you are passing are references and not values to your function, that does not consumes memory because you are not copying your variables (but increases memory by increasing stack frames though). If your objects are referenced in a thread stack then there is no way it gets overwritten.

    [...]or if the case were that there is no memory, I would get some exception that the program memory has run out

    You will get an Asynchronous exception (OutOfMemoryError) in case of the JVM depleted its memory but here the only exception you would get is a StackOverflowError if your recursion function calls itself a huge number of times.

    0 讨论(0)
  • 2020-12-30 12:08

    There are two storage areas involved: the stack and the heap. The stack is where the current state of a method call is kept (ie local variables and references), and the heap is where objects are stored. The Hotspot documentation says that on Linux 64-bit each thread has a stack of 1024kB by default. The heap can be made arbitrary big, and today it's in the order of GB.

    A recursive method uses both the stack and the heap. Which one you run out of first depends on the implementation. As an example, consider a method which needs thousands of integers: if they are declared as local variables, ie:

    public void stackOverflow() {
      int a_1;
      int a_2;
      int a_3;
      // ...
      int a_10_000_000;
    }
    

    your program will crask with a StackOverflowError. On the other hand, if you organize your integers in an array, like:

    public void outOfMemory() {
      int[] integers = new int[10 * 1000 * 1000];
    } 
    

    the heap will be filled soon, and the program will end with an OutOfMemoryError. In neither case the memory is corrupted or data overridden. However, in both cases the code is wrong and must be fixed somehow - but to tell you how we'd need to know more about your program.

    0 讨论(0)
提交回复
热议问题