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 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:
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.
posted on 2013-12-20 01:30 Step-BY-Step 阅读(151) 评论(0) 编辑 收藏 举报