+ 2
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
2 Answers
+ 2
Hello Jason Stone
You are using while(true). Did you control the method if it is returning a value?
0
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.