[prev in list] [next in list] [prev in thread] [next in thread] 

List:       httpclient-users
Subject:    Re: Correct resource deallocation with HttpClient 4.3.x
From:       Oleg Kalnichevski <olegk () apache ! org>
Date:       2016-05-19 9:32:53
Message-ID: 1463650373.24193.5.camel () apache ! org
[Download RAW message or body]

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

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic