2017-08-03 130 views
1

我有以下代码片段。在调用端点时返回图像的简单服务。泽西岛内存泄漏?

@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次/秒,持续几秒钟,在我们的搬运工容器天空的内存使用量猛增(约300至在演出),我们已经设置了XMX到512但内存不断攀升。

我失去了一些东西在这里?我们正在使用Dropwizard和Jersey。

回答

0

运行一个探查器后,我发现我们正在创建数千个对象来响应FooService中的这个调用。我们能够把它变成一个单身人士,而且似乎已经解决了很多问题。