About cURL (for Client URL):
PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols.
Source: https://www.php.net/manual/en/intro.curl.php.
Any cURL session must be:
curl_init()
.curl_close()
in order to free up system resources.What is the impact of not explicitly calling curl_close()
?
Will it generate memory leaks?
TL;DR: PHP memory management will properly clean unclosed sessions at process exit.
The following is the result of some investigations in PHP internals.
PHP version used at time of writing is 7.4.2.
When cURL package functions are registered (using PHP_MINIT_FUNCTION extension function), a destructor _php_curl_close()
associated to resource type "curl" is registered, using function zend_register_list_destructors_ex()
:
More information at page Playing with resources.
Basic workflow of a cURL session:
Initialize cURL session by calling curl_init()
.
Many sub-functions calls will be made:
Call to curl_easy_init()
.
Call to zend_register_resource()
.
This will allow destruction of handle ch
at the end of the process.
Subsequent calls to cURL functions using handle returned at step 1.
Close cURL session by calling curl_close()
.
If curl_close()
function is not called, the session handle will stay in the list of resource types (as registered at step 1.3).
At the end of the process, items of this list will be fetched by PHP interpreter and deleted using their related destructors.