Java OkHttp:避免泄漏连接警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38380371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:15:46  来源:igfitidea点击:

OkHttp: avoid leaked connection warning

javamemory-leaksokhttp3

提问by Alphaaa

I am using OkHttp 3, and I keep getting leaked connection warnings:

我正在使用 OkHttp 3,并且不断收到泄漏的连接警告:

WARNING: A connection to https://help.helpling.com/ was leaked. Did you forget to close a response body?
Jul 14, 2016 6:57:09 PM okhttp3.ConnectionPool pruneAndGetAllocationCount

Everytime I get a ResponseBody, I either call .string()which supposedly closes the stream for me, or I explicitly close it in a finallyblock, in the following way:

每次我得到 a 时ResponseBody,我要么调用.string()它为我关闭流,要么finally通过以下方式在块中显式关闭它:

ResponseBody responseBody = response.body();
try (Reader responseReader = responseBody.charStream()) {
    ...
}
finally {
    responseBody.close();
}

My application makes intense use of the network, and yet that warning appears frequently. I never observed any problem caused by this presumed leak, but I would still like to understand ifand whatI am doing wrong.

我的应用程序大量使用网络,但该警告经常出现。我从未观察到由这种假定的泄漏引起的任何问题,但我仍然想了解我是否做错了以及我做错了什么

Could anyone shed some light on this?

任何人都可以对此有所了解吗?

采纳答案by Alphaaa

By upgrading to OkHttp 3.7, Eclipse started warning me of potential resource leaks. I found my problem to be in this method I wrote:

通过升级到 OkHttp 3.7,Eclipse 开始警告我潜在的资源泄漏。我发现我的问题出在我写的这个方法中:

public static Response getResponse(HttpUrl url, OkHttpClient client) throws IOException {
    Builder request = new Request.Builder().url(url);
    Response response = client.newCall(request.build()).execute();
    if (!response.isSuccessful()) {
        boolean repeatRequest = handleHttpError(response);
        if (repeatRequest)
            return getResponse(url, client, etag);
        else
            throw new IOException(String.format("Cannot get successful response for url %s", url));
    }
    return response;
}

I assumed that by always calling getResponse(url, client).body().string()the stream would close automatically. But, whenever a response was unsuccessful, an exception would raise before the execution of .string(), thus the stream would remain open.

我认为通过始终调用getResponse(url, client).body().string()流会自动关闭。但是,只要响应不成功,就会在执行之前引发异常.string(),因此流将保持打开状态。

Adding an explicit close in case of unsuccessful response solved the problem.

在响应不成功的情况下添加显式关闭解决了问题。

if (!response.isSuccessful()) {
    boolean repeatRequest = handleHttpError(response);
    response.close();
}

回答by Corrigan Johnson

As mentioned in the other answers, you have to close the response. A slightly cleaner approach would be to declare the ResponseBodyin the try block, so that it will be automatically closed.

正如其他答案中所述,您必须关闭响应。一种稍微简洁的方法是ResponseBody在 try 块中声明,以便它会自动关闭。

try(ResponseBody body = ....){
....
}