2

When I try to use okhttp to get content from web sites, I found that I am unavailable to manually close the established sessions (using netstat to check), of course I know this is one of feature that natively support by okhttp, but in my case, the project should require to release sessions same as using HTTPURLConnection.close() methods, and I cannot find in okhttpclient.

How can I do it? thanks.

Andy Chan
  • 277
  • 5
  • 16

1 Answers1

3

Try this:

okHttpClient.connectionPool().evictAll();
Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • Oh very good! it works! and I have questions that will the action waste system resource in either CPU/Thread/Memory? because I am not so understand when every okhttpclient(s) are created, what system resources will be consume? it is java's Executor? – Andy Chan Jun 11 '16 at 18:23
  • For that you'll need to study the code. Clearing connection pools is good but only if you never plan to reuse any connections. – Jesse Wilson Jun 12 '16 at 21:03
  • That’s all of the samples. Create one OkHttpClient and reuse it. – Jesse Wilson Jun 14 '16 at 02:02
  • OK, I can reuse now! – Andy Chan Jun 19 '16 at 05:02