Search code examples
memoryintellij-ideastack-overflowheap-memory

How to postpone stackoverflowError IntelliJ IDEA?


I found a solution where I didn't have to increaseany memory size,

var count = 0

fun main(args: Array<String>) {
    repeater()
}
fun repeater(){
    a()
    repeater()
}
fun a(){
    if(count%2000==0 && count !=0){
        count++
        return
    }
    count++
    println(count)
}

this actually works for some reason. Testing it with the simple println() it went up from the 1100 to over 400k. I just recently started programming and I dont quite understand this, also testing it in my program where 2 bots play against each other it did increase the matches they could play from 400 to about 4000-5000. (I had to play with different modulators(?) to find the optimal ) I also tested using more than one repeater and modulator-ifs, but I couldn't find any rule, with some additional it was way better, with some even worse.

//this is the original question:

I want the error to be thrown later, so if it were thrown after 1000 recursive calls I would want it to be thrown after 2000 or more. Increasing heap memory or memory doesn't change it. Always around 11000 calls for me.

var count = 0   
fun a(){  count++  println(count)  a() }

Solution

  • Increase the thread stack size using the -Xss JVM argument.

    From the documentation:

    -Xsssize

    Sets the thread stack size (in bytes). Append the letter k or K to indicate KB, m or M to indicate MB, g or G to indicate GB. The default value depends on the platform:

    • Linux/ARM (32-bit): 320 KB

    • Linux/i386 (32-bit): 320 KB

    • Linux/x64 (64-bit): 1024 KB

    • OS X (64-bit): 1024 KB

    • Oracle Solaris/i386 (32-bit): 320 KB

    • Oracle Solaris/x64 (64-bit): 1024 KB

    The following examples set the thread stack size to 1024 KB in different units:

    -Xss1m
    -Xss1024k
    -Xss1048576
    

    This option is equivalent to -XX:ThreadStackSize.