hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Correct resource deallocation with HttpClient 4.3.x
Date Thu, 19 May 2016 09:32:53 GMT
On Tue, 2016-05-17 at 23:28 +0300, Michael Velbaum wrote:
> Hi,
> 
> I have a few questions regarding proper resource de-allocation, as this
> topic is not covered in detail in the tutorial.
> 
> 1. The CloseableHttpClient interface allows you to close an HttpClient.
> What advantage does manually closing an HttpClient have over simply letting
> the HttpClient object be garbage collected when I'm done with it?
> 

It ensures immediate shutdown and deallocation of system resources
allocated by persistent (kept-alive) connection.

> 2. Do I need to use a CloseableHttpResponse and manually close the response
> if I already use EntityUtils.toByteArray(entity) to fully consume the
> response?
> 

Yes, you do. You should always close responses with try-finally or
try-with-resources to make sure resources allocated by the response get
deallocated even in case of an exception.

If the response content has been fully consumed by the time
CloseableHttpResponse#close is called the overhead of the #close method
will be literally a true/false check on a volatile boolean variable.

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message