OkHttp:避免泄露连接警告

Alp*_*aaa 28 java memory-leaks okhttp3

我正在使用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
Run Code Online (Sandbox Code Playgroud)

每次我得到一个ResponseBody,我要么调用.string()哪个可以为我关闭流,或者我在一个finally块中明确地关闭它,方法如下:

ResponseBody responseBody = response.body();
try (Reader responseReader = responseBody.charStream()) {
    ...
}
finally {
    responseBody.close();
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序大量使用网络,但这种警告经常出现.我从来没有看到造成这种推测泄漏的任何问题,但我还是想知道,如果什么我做错了.

谁能对此有所了解?

Alp*_*aaa 13

通过升级到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;
}
Run Code Online (Sandbox Code Playgroud)

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

在响应失败的情况下添加显式关闭可以解决问题.

if (!response.isSuccessful()) {
    boolean repeatRequest = handleHttpError(response);
    response.close();
}
Run Code Online (Sandbox Code Playgroud)


小智 11

如其他答案中所述,您必须关闭响应.稍微清洁的方法是ResponseBody在try块中声明,以便它将自动关闭.

try(ResponseBody body = ....){
....
}
Run Code Online (Sandbox Code Playgroud)