Home > Back-end >  i am trying to print a table in infinite loop in kotlin but getting this (Exception in thread "
i am trying to print a table in infinite loop in kotlin but getting this (Exception in thread "

Time:12-11

need help to resolve this error Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

 Fun main(){
    Val num = 2
    Var i = 1
    while(i >= 1)
    {
    Println(num * i)
    }
    }

CodePudding user response:

Value of i is always 1 and you are going in infinite loop so that's why out of memory is coming try to change the condition and you will get the output hope this helps

Fun main(){
    Val num = 2
    Var i = 1
    while(i >= 1)
    {
    Println(num * i)
    i=i-1
    }
    }

CodePudding user response:

Calling println kicks off some internal operations that use memory, and if you spray a lot of them quickly enough, you can get an OOM (before garbage collection has a chance to run, I'm assuming)

exhibit A

fun main() {
    while (true) {
        println("uh oh")
    }
}

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
 at java.lang.StringCoding$StringDecoder.decode (StringCoding.java:149) 
 at java.lang.StringCoding.decode (StringCoding.java:193) 
 at java.lang.StringCoding.decode (StringCoding.java:254) 
  • Related