我有以下代码段。调用端点时返回图像的简单服务。
@GET
@Path("/categories/{category}/image")
@Produces("image/jpeg")
@UnitOfWork
public StreamingOutput getCategoryImage(@PathParam("category")Category category){
//foo service will return an Optional
return fooService.getImage(category).map(new Function<InputStream, StreamingOutput>() {
@Override
public StreamingOutput apply(InputStream inputStream) {
return (StreamingOutput) output -> BarResource.this.copyAndClose(inputStream, output);
}
})
.orElseThrow(NotFoundException::new);
}
//Originally this method did not exist, but I am trying this to close the 'leak'
private long copyAndClose(InputStream inputStream, OutputStream outputStream) throws IOException{
try(InputStream temp = inputStream; OutputStream tempOut = outputStream) {
return IOUtils.copy(temp, tempOut);
}
}
然而,通过压力测试,我们将这个1600次/秒调用了几秒钟,我们的docker容器天空中的内存使用率飙升(从大约300到超过一个演出)我们已经将Xmx设置为512但是内存继续攀登。
我在这里遗漏了什么吗?我们正在使用Dropwizard和Jersey。
答案 0 :(得分:0)
运行探查器后,我发现我们在FooService中响应此调用创建了数千个对象。我们能够使它成为一个单身人士,这似乎解决了很多问题。