为什么BigInteger最终在Java中的StackOverflowError位于斐波那契序列中的19635位

为什么BigInteger最终在Java中的StackOverflowError位于斐波那契序列中的19635位,java,stack-overflow,biginteger,fibonacci,Java,Stack Overflow,Biginteger,Fibonacci,我使用下面的代码来计算斐波那契序列到任意大的数字。代码按预期工作,直到我要求它调用函数4000000次(以前我只调用了几百次),计算机运行了一段时间,然后Eclipse中的控制台生成异常,我将其粘贴在代码下面 我的问题是这里到底发生了什么?我的计算机是否完全耗尽了内存或分配给线程的内存?如果是的话,为什么它会停在这个数字上 附加:如何计算斐波那契序列的更多数字 import java.math.BigInteger; public class fibonacci2 { public stat

我使用下面的代码来计算斐波那契序列到任意大的数字。代码按预期工作,直到我要求它调用函数4000000次(以前我只调用了几百次),计算机运行了一段时间,然后Eclipse中的控制台生成异常,我将其粘贴在代码下面

我的问题是这里到底发生了什么?我的计算机是否完全耗尽了内存或分配给线程的内存?如果是的话,为什么它会停在这个数字上

附加:如何计算斐波那契序列的更多数字

import java.math.BigInteger;

public class fibonacci2 {

public static void main(String[] args) {

    calculate(p,q);

}
static long i = 0;
static BigInteger p = BigInteger.valueOf(0);
static BigInteger q = BigInteger.valueOf(1);
static BigInteger temp = BigInteger.valueOf(1);

public static void calculate(BigInteger s,BigInteger t){
    while(i<4000000){       
        System.out.printf("%d\t%d\n", p, i);
        temp = p;
        p = p.add(q);
        q = temp;
        i++;
        calculate(p,q);
        }
    }

}
生成的最后一个数字:19635-相当大的数字


1326878955489024661813666359786489363862597596267841591984718441133687207077625589984392446213667083626600221932584888435387175864913621007699074488726146553396344400571464483789445258320301817159170109724593316662621253046333396412482529462637582636363636363736363737343433745454545457562626262627373738282827373737373737346464646468282282847464646464646465252828282822847474746464646515151515173737373737373737373737346464646464646467215296795469714967907029177585255931953041097074190910785453226029971398760408984833172078432454184605198390087994150395060285572150251583165471868389499945672843481685947515587743034377499766143532077192661721528003993359997246530934525456717833943628095217461954182586288476707560754969065064084782194766518458888910291084801007757831815520947787740120755811742462919387767321622766630079266602618858106961596843988507661304667498922379613689785373506595033848871845907280299389403460296648540983323252760758030046686839736639119651662446556232889766693533926844937732565382864614598511099853165942185670363311106201665037520028589603459480093687209129483990019109169869972202653624366647857999434391094762018179263283077871019031113939615673588007412539751140896660550555242286410356957225761374959188921428971568986925032771140869722035908453941369766279904773927026581100216330293759284523441759616193424464255776920179198282898929651696084392141850582592994281421363929270876943636066323962007120387628464011461761740762357314279995093415406703654359369624210821189574276573351209405725688095531085757790385956457905674653179873849894181307352153521970444549381840418885602398030186861851646982941912175759123009610414712538463790168381115394585031993406508479251708300413089091046202402547272127194856959912336070123054000971528209301706716799967865863566308741161048855000242554594776051450015602530130830632799865916436910426907366815701787618197467431739535541208421734061411825946504001994253317591124427008430838958355344794310454651478239057184460401880272193690440447468818908709573559416973460476456159514222828127654757419791409169485455441358915909849944213162535650990628728567205008021505292674768149264242301644153506070181242238265060736978048559868387956160618589900694144893516842056223562659983574582770029522671424631473401781040296472536986065247377619913145937120795036202439815288145371316378406620804256354366292568284277601526223052039161567965208997051741677342969084084514529962670562971904235724951300259522336727790146934482140537946422666803487387530768149156865520490977449591331837555541175998073317171279932294605244447052934146691113860078257087608146745653933331414077123378851102661024543536267021765035186200512349488244031289740166466522193438740868197795979020800115878227245915721611276331619834854207870999915064756496491413486357648666112374600381616094828115054590567209841647733123062265193776874225929913729094158671891155728734269423508352994181964483014546369289457638937119486425219394441724989757668029639003653716033219392285370690050447276977399083217385116221597190902231573954851311174880010350107709598222713116149040027547310711433291275201161099258049003651135704238807721959550159546836643230735737588000632853461610985842303637091707673006651241119081362122198822561880151457438138335669932411369209479697090744946937084004901652666265244380958229131979018534241425529146657033257215884143397532866216431133801685191737047942999200701903636425929595099028157690959476070093958185643947614048080830681082316690386212711757251296571629330858314762376634972295422204411354049398435246045511254655517277718983900530833320188511414211146698090535225453522966541012204143824248453649100578724828688310348160123621209458528951047344255833328038412893164379387055901690802972739928114327516209154924343433813058215903873087748456231395146808816246705104628538431451588404880679735578696218026459996037318513644271555799763221584367218460891732898265796640381712184738676189794477950057082407328019657448436514571121591888448068724038850746307617362060268368290796959839205181778917655303213810001531766217479373389669659109054686337516427263534264878658023969472873886179643838385931023490与
大整数无关,发生这种情况是因为
calculate()
方法正在调用自身。一旦调用堆栈足够深,内存就会耗尽,JVM就会抛出
stackOverflowerError

calculate()
方法中没有任何类型的终止条件。每次输入
while
循环时,都会启动另一个调用

事实上,您似乎试图同时迭代和递归地解决这个问题

代码中不应该同时需要while循环和递归调用。在我看来,如果您只需调用
calculate()
您的程序就能正常运行:

public static void calculate(BigInteger s, BigInteger t) {
    while (i < 4000000) {
        System.out.printf("%d\t%d\n", p, i);
        temp = p;
        p = p.add(q);
        q = temp;
        i++;
        // calculate(p, q); // you don't need this
    }
}
publicstaticvoidcalculate(大整数s,大整数t){
而(i<4000000){
System.out.printf(“%d\t%d\n”,p,i);
温度=p;
p=p.add(q);
q=温度;
i++;
//计算(p,q);//你不需要这个
}
}

正如其他人所指出的,发生错误是因为您使用的是递归,而不是r
public static void calculate(BigInteger s, BigInteger t) {
    while (i < 4000000) {
        System.out.printf("%d\t%d\n", p, i);
        temp = p;
        p = p.add(q);
        q = temp;
        i++;
        // calculate(p, q); // you don't need this
    }
}