递归和记忆 [英] recursion and memory

查看:118
本文介绍了递归和记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序通过递归传递大量数据,比如1000个变量。递归将至少运行50或60次。我担心的是,是否存在数据被覆盖在内存位置上的可能性,因为没有太多空间,或者如果情况是没有内存,我会得到程序内存的异常已经用完了(我没有收到这样的错误)?

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?

推荐答案

涉及两个存储区域:堆栈和堆。堆栈是保存方法调用的当前状态的地方(即局部变量和引用),堆是存储对象的位置。 的热点文档说,在Linux 64位的每个线程都有一个默认情况下,堆栈为1024kB。堆可以任意大,今天它的大小是GB。

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;
}

您的程序将以 StackOverflowError 。另一方面,如果您在数组中组织整数,例如:

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];
} 

堆将很快填满,程序将以<$结束C $ C>的OutOfMemoryError 。在这两种情况下,内存都已损坏或数据被覆盖。但是,在这两种情况下,代码都是错误,必须以某种方式修复 - 但要告诉您如何我们需要了解有关您的计划的更多信息。

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.

这篇关于递归和记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆