我到处寻找,找不到可靠的答案.根据文档,Java 在以下情况下抛出java.lang.StackOverflowError错误:
在发生堆栈溢出时抛出,因为应用程序过于严重.
但这提出了两个问题:
详细说明第二个问题:
当Java抛出StackOverflowError时,你能安全地假设堆栈没有写入堆中吗?如果你在一个抛出堆栈溢出的函数的try/catch中缩小堆栈或堆的大小,你能继续工作吗?这记录在哪里?
答案我不是在寻找:
为了纪念Stack Overflow的公开发布,导致堆栈溢出的最短代码是什么?任何语言欢迎.
ETA:只是要明确这个问题,因为我偶尔会看到一个Scheme用户:尾调用"递归"实际上是迭代,任何可以通过合适的编译器相对简单地转换为迭代解决方案的解决方案都不会算一算 :-P
ETA2:我现在选择了"最佳答案"; 看这篇文章的理由.感谢所有贡献的人!:-)
我很惊讶即使StackOverflowError在Java发生之后仍然可以继续执行.
我知道这StackOverflowError是类Error的子类.类Error被称为"Throwable的一个子类,表示一个合理的应用程序不应该试图捕获的严重问题".
这听起来更像是一个推荐而不是一个规则,主张捕获像StackOverflowError这样的错误实际上是允许的,这取决于程序员不这样做的合理性.看,我测试了这段代码,它正常终止.
public class Test
{
public static void main(String[] args)
{
try {
foo();
} catch (StackOverflowError e) {
bar();
}
System.out.println("normal termination");
}
private static void foo() {
System.out.println("foo");
foo();
}
private static void bar() {
System.out.println("bar");
}
}
Run Code Online (Sandbox Code Playgroud)
怎么会这样?我想当抛出StackOverflowError时,堆栈应该是如此完整,以至于没有空间调用另一个函数.错误处理块是在不同的堆栈中运行,还是在这里发生了什么?
StackOverflowError和OutOfMemoryError之间的区别是什么以及如何在应用程序中避免它们?
我正在编写一个函数,可以调用自己大约5000次.当然,我得到了一个StackOverflowError.有什么方法可以用相当简单的方式重写这段代码吗?:
void checkBlocks(Block b, int amm) {
//Stuff that might issue a return call
Block blockDown = (Block) b.getRelative(BlockFace.DOWN);
if (condition)
checkBlocks(blockDown, amm);
Block blockUp = (Block) b.getRelative(BlockFace.UP);
if (condition)
checkBlocks(blockUp, amm);
//Same code 4 more times for each side
}
Run Code Online (Sandbox Code Playgroud)
那么,我们可以称之为功能的深度有多大限制?
我写过 - 似乎是 - 在Java和C++中完全相同的继承示例.看到这些计划的不同产出,我感到非常惊讶.让我分享代码片段和相应的输出.
C++代码:
class A
{
public:
A() {}
void sleep() {
cout << "A.Sleep" << endl;
eat();
}
void eat() {cout << "A.Eat" << endl;}
};
class B: public A
{
public:
B() {}
void sleep() {
A::sleep();
cout << "B.Sleep " <<endl;
this->eat();
}
void eat() {
cout << "B.Eat" << endl;
run();
}
void run() {
A::sleep();
cout << "B.run" << endl;
}
};
int main()
{
B *b = new B();
b->sleep();
} …Run Code Online (Sandbox Code Playgroud) 可能重复:
什么是堆栈溢出错误?
任何人都可以告诉我如何以及为什么在程序中实际发生堆溢出和堆溢出,以及如何克服编程中的堆栈溢出 - 如何避免它?
我正在使用ondragListener视图拖动.我的root是viewgroup在根目录中有更多viewgroup容器,并且正在添加imageview/textview容器内部的视图.
如果我使用唯一的容器(ViewGroup)linearlayout来拖放它的工作正常,但如果我开始添加像imageview这些容器内的视图,仍然拖动点是只持有此视图的容器.
onActionDrop得到一个stackoverflow error : stack size 8MB.
onActionDrop 代码是:
if (e.getAction()==DragEvent.ACTION_DROP) {
final View view = (View) e.getLocalState();
final ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
final LinearLayout toView = (LinearLayout) v;
toView.addView(view);}
Run Code Online (Sandbox Code Playgroud)
错误日志:
java.lang.StackOverflowError: stack size 8MB
android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6546)
android.os.TransactionTooLargeException: data parcel size 26840220 bytes
android.os.BinderProxy.transactNative(Native Method)
android.os.BinderProxy.transact(Binder.java:503)
android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4425)
com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:90)
java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
Run Code Online (Sandbox Code Playgroud) 我编写了一个如下所示的小程序,该程序计算无限递归循环在导致StackOverflow错误之前将经过多少次。
public class Testing {
static void p(int i) {
System.out.println("hello" + i);
i++;
p(i);
}
public static void main(String[] args) {
p(1);
}
}
Run Code Online (Sandbox Code Playgroud)
事实是,它每次都会以不同的数字出错,通常在8000到9000之间。有人能解释为什么会这样吗?
编辑:我正在使用Eclipse IDE,尚未与其他IDE或命令行对其进行测试。
这是我的计划的背景.
一个函数有50%的机会什么都不做,50%自称两次.该计划完成的概率是多少?
我写了这段代码,显然效果很好.对每个人来说可能并不明显的答案是,该计划有100%的机会完成.但是当我运行这个程序时,会出现一个StackOverflowError(有多方便;)),在Math.Random()中出现.有人能指出我从哪里来,并告诉我,如果我的代码可能是错的?
static int bestDepth =0;
static int numberOfPrograms =0;
@Test
public void testProba(){
for(int i = 0; i <1000; i++){
long time = System.currentTimeMillis();
bestDepth = 0;
numberOfPrograms = 0;
loop(0);
LOGGER.info("Best depth:"+ bestDepth +" in "+(System.currentTimeMillis()-time)+"ms");
}
}
public boolean loop(int depth){
numberOfPrograms++;
if(depth> bestDepth){
bestDepth = depth;
}
if(proba()){
return true;
}
else{
return loop(depth + 1) && loop(depth + 1);
}
}
public boolean proba(){
return Math.random()>0.5;
}
Run Code Online (Sandbox Code Playgroud)
.
java.lang.StackOverflowError
at java.util.Random.nextDouble(Random.java:394)
at java.lang.Math.random(Math.java:695)
Run Code Online (Sandbox Code Playgroud)
.我怀疑堆栈及其中的功能数量是有限的,但我真的没有看到这里的问题. …