In OkHttp what is the difference between response.close() and response.body.close()? - Stack Overflow

admin2025-04-16  4

I am working with OkHttp for my connections. To avoid memory leaks by closing responses. But the docs and none of the information I found explains which close to use.

Here are the difference close functions I've found

response.close()
response.body.close()
response.peekBody(Int).close()
response.cacheResponse.close()
responseworkResponse.close()

and there's probably more. But it seems like the big one is response.close() which is fundamentally different from the others. Do I need to close the whole thing or just the body to avoid memory leaks?

I am working with OkHttp for my connections. To avoid memory leaks by closing responses. But the docs and none of the information I found explains which close to use.

Here are the difference close functions I've found

response.close()
response.body.close()
response.peekBody(Int).close()
response.cacheResponse.close()
response.networkResponse.close()

and there's probably more. But it seems like the big one is response.close() which is fundamentally different from the others. Do I need to close the whole thing or just the body to avoid memory leaks?

Share asked Feb 2 at 6:33 SMBiggsSMBiggs 11.7k6 gold badges74 silver badges89 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

From the Response docs:

This class implements Closeable. Closing it simply closes its response body. See ResponseBody for an explanation and examples.

转载请注明原文地址:http://www.anycun.com/QandA/1744808389a87907.html