메모리 제한이 64 MB로 넉넉치 않아서 단순히 readln()을 사용하면 메모리 초과가 발생합니다.
메모리 초과가 발생하지 않도록 입력을 받는 방법 공유드립니다.
import java.util.*
fun main() {
val scanner = Scanner(System.`in`.buffered(bufferSize = 4 * 1024))
val n: Int = scanner.nextInt()
val a: Long = scanner.nextLong()
val b: Long = scanner.nextLong()
val c: Long = scanner.nextLong()
val x = IntArray(size = n) {
gcIfNeeded()
scanner.nextInt()
}
scanner.close()
// TODO 구현
}
private fun gcIfNeeded() {
val runtime = Runtime.getRuntime()
if (runtime.totalMemory() - runtime.freeMemory() > 64 * 1024 * 1024) {
runtime.gc()
}
}
boxresin 1년 전 1
메모리 제한이 64 MB로 넉넉치 않아서 단순히
readln()을 사용하면 메모리 초과가 발생합니다.메모리 초과가 발생하지 않도록 입력을 받는 방법 공유드립니다.
import java.util.* fun main() { val scanner = Scanner(System.`in`.buffered(bufferSize = 4 * 1024)) val n: Int = scanner.nextInt() val a: Long = scanner.nextLong() val b: Long = scanner.nextLong() val c: Long = scanner.nextLong() val x = IntArray(size = n) { gcIfNeeded() scanner.nextInt() } scanner.close() // TODO 구현 } private fun gcIfNeeded() { val runtime = Runtime.getRuntime() if (runtime.totalMemory() - runtime.freeMemory() > 64 * 1024 * 1024) { runtime.gc() } }