10

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, is there a possibility of data getting over- written on the memory locations because there isn't much space, or if the case were that there is no memory, I would get some exception that the program memory has run out (I received no such error)?

Is there a possibility of getting a wrong solution because the program does not have any more memory and is over-writing on existing locations?

Floose
  • 525
  • 2
  • 7
  • 15

5 Answers5

23

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.

eleven
  • 6,779
  • 2
  • 32
  • 52
Raffaele
  • 20,627
  • 6
  • 47
  • 86
5

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.

ElderMael
  • 7,000
  • 5
  • 34
  • 53
  • and in each of these recursions, if I was creating a copy of the variables, there would still be no over-writing? – Floose Nov 24 '12 at 20:21
  • Overwritting no, but how are you passing your variables? are you sure are you creating, passing or copying? What do you mean by copying? – ElderMael Nov 24 '12 at 20:23
  • I meant a deep copy .. but yes, I discovered the bug in my code.. thanks for all your help – Floose Nov 26 '12 at 17:47
2

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.

Haile
  • 3,120
  • 3
  • 24
  • 40
1

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

Jatin
  • 31,116
  • 15
  • 98
  • 163
  • 1
    To be precise, it is a "StackOverflowError", not an exception. Errors and exceptions are not the same! "An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions" – Haile Nov 24 '12 at 19:48
  • It was a slip of typing. Corrected. Thanks – Jatin Nov 24 '12 at 19:49
1

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

Mukul Goel
  • 8,387
  • 6
  • 37
  • 77