2017-08-03 125 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中的這個調用。我們能夠把它變成一個單身人士,而且似乎已經解決了很多問題。