New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation OkHttp 3: Highlight that one must not recreate the OkHttpClient for each request #2636
Comments
Wanna send a pull request? |
Looking at your samples from https://github.com/square/okhttp/tree/master/samples it seems that you are creating the OkHttpClient at the same time with the Request sent to the client. Do you think the samples should be changed too? Based on my experience, it's just a really bad practice to create the OkHttpClient on-demand with the Request. Or do you think it's a bug that the file handlers are not released? Thus, creating the OkHttpClient on-demand should be ok? |
I would probably at least add a new section to your FAQ in the Wiki, if for nothing else, for Google to find it for other people if the stumble on the same error message. https://github.com/square/okhttp/wiki/FAQs
English is not my native language, so feel free to edit as seen fit, if you find this useful. |
The bug I encountered is due to the fact that I created a new Create a default This will keep the connections around for at least 5 minutes: If we don't want to force the user to create a custom |
I think we fix this with documentation. Which docs did you read before making that mistake? |
Well, neither the examples at http://square.github.io/okhttp/ nor the Wiki say anything about this corner case. Also the samples in GitHub do show that the client is created together with the request. If there's documentation about ConnectionPool request cleanup and how not to use the OkHttpClient, I must have missed it. |
I would also like to point out that the comment about |
👍 to these doc improvements! Just ran into this issue and was able to easily figure it out thanks to these changes. |
With OkHttp 3.3.1 if one is recreating the OkHttpClient for each request like so:
OkHttpClient client = new OkHttpClient.Builder().build();
you will run out of file handles for your app. Your application will terminate with the following error message after a while
It is mentioned in the OkHttp 3 release notes that one should avoid recreating the
OkHttpClient
because the app may run out of memory due to each client getting its own thread pool.But in my experience, one definitely should not recreate the
OkHttpClient
object because of the issue with file handles, which is much harder to debug and link to OkHttp 3.On the other hand, if the
OkHttpClient
object is going out of scope and being garbage collected by the system (assumably), it may also be a bug that the thread pool is not cleaned and file handles not being closed properly.The text was updated successfully, but these errors were encountered: