我正在使用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
每次我得到一个 ResponseBody 时,我都会调用 .string() 来关闭流,或者以如下方式显式地在 finally 块中关闭它:
ResponseBody
.string()
finally
ResponseBody responseBody = response.body(); try (Reader responseReader = responseBody.charStream()) { finally { responseBody.close(); }
我的应用程序对网络进行了大量的使用,但是这个警告经常出现。我从来没有观察到由这个假定的泄漏引起的任何问题,但我仍然想知道我是否做错了什么。
有人能解释一下这件事吗?
【签约计划】腾讯云测评官奖励活动
加入我们,和腾讯云一起探索云上世界的无限可能!
通过升级到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); throw new IOException(String.format("Cannot get successful response for url %s", url));