recursion lead to out of memory

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:

publicvoid *(){int a_1;int a_2;int a_3;// ...int a_10_000_000;}

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

publicvoid outOfMemory(){int[] integers =newint[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.

Solution:

need to switch to a disk based structure, a database or a memory mapped hashtable.

上一篇:CSS标准文档流 块级元素和行内元素


下一篇:JS*日历