
[英]What actually is “java.lang.OutOfMemoryError: Java heap space” and how can i increase the Memory for Java?
[英]How can I handle "java.lang.OutOfMemoryError: Java heap space" in reactor?
我使用 reactor 和 Webflux 实现了我的简单休息端点。
interface CsvRepository : ReactiveCrudRepository<CsvRow, Long>
@Service
class CsvService(val csvRepository: CsvRepository) {
fun createByteArray(): Mono<ByteArray> =
csvRepository.findAll()
.reduce(ByteArrayOutputStream()) { output, el ->
output.write(el.toString().toByteArray())
output.write("\n".toByteArray())
output
}
.map { it.toByteArray() }
}
@SpringBootApplication
class WebfluxMemoryRetroProjectApplication(val csvService: CsvService) {
@Bean
fun routing(): RouterFunction<ServerResponse> = router {
accept(MediaType.ALL).nest {
GET("/test") {
csvService.createByteArray()
.flatMap { result ->
ServerResponse.ok()
.headers { httpHeaders ->
httpHeaders.contentType = MediaType("application", "force-download")
httpHeaders.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=test.txt")
}
.bodyValue(ByteArrayResource(result))
}
}
}
}
}
作为 HTTP GET 请求的结果,它从数据库请求数据并创建一个大小约为 70MB 的文件。 所以如果我用 -xmx100m 运行它,我的日志中只有一个错误:
2022-02-07 12:25:06.135 ERROR 10288 --- [actor-tcp-nio-1] r.n.channel.ChannelOperationsHandler : [7edd71ea, L:/127.0.0.1:58918 - R:localhost/127.0.0.1:5432] Error was received while reading the incoming data. The connection will be closed.
java.lang.OutOfMemoryError: Java heap space
....
2022-02-07 12:25:06.141 ERROR 10288 --- [actor-tcp-nio-1] i.r.p.client.ReactorNettyClient : Connection Error
reactor.netty.ReactorNetty$InternalNettyException: java.lang.OutOfMemoryError: Java heap space
未处理此错误并且 HTTP 客户端请求挂起。 这是预期的行为吗? 我该如何处理这个异常? 我试过onErrorResume
和Hooks.onOperatorError
但我无法拦截这种类型的异常。 对我来说,应用程序继续工作并且我无法拦截和处理此类错误似乎不合逻辑。 重现错误的项目: https ://github.com/typik89/webfluxMemoryRetroProject/blob/main/src/main/kotlin/ru/typik/reactor/WebfluxMemoryRetroProjectApplication.kt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.