Kotlin unexpectedly running out of memory | Sololearn: Learn to code for FREE!


Kotlin unexpectedly running out of memory

So, normal kotlin codes work fine, but this one I made is unexpectedly running out of memory. I’ve run it on my iMac using both Java: $ kotlinc ./myKotlinProgram.kt -d ./myKotlinProgram.jar $ java -jar ./myKotlinProgram.jar and Safari (using Kotlin/JS) and it had no memory leaks as expected. https://code.sololearn.com/cuikJheZ798r/?ref=app

4/28/2020 3:52:48 AM

Jason Stone

2 Answers

New Answer


Hello Jason Stone You are using while(true). Did you control the method if it is returning a value?


Denise Roßberg I rewrote the trampline function to not use the while(true) and instead work recursively with tailrec. It’ll compile to essentially the same thing, but is perhaps a bit clearer and communicates it’s purpose more effectively. Also, as I said above, I tested it on my computer and it worked as expected (no extra memory use per iteration of the recursive factorial) when compiled to .jar and run with Java. And I know that it’s not using memory for every iteration because I cranked up the iteration count until it took 5 or 10 seconds to execute and it never used even a single gigabyte despite iterating to a depth of one billion which should use 1 gigabyte at 1 byte per depth (although 16 or so bytes would be far more likely, resulting in 16 gigabytes usage). I know that it must be using memory for every recursion in the SoloLearn playground because that’s the only thing that would require so much memory. Either that or SoloLearn’s Java is broken and not properly garbage collecting.