I am using laravel 5.5.*
and guzzlehttp/guzzle ^6.3
. I have created APIs in the same project ( using laravel api.php
) and consuming API on the same project ( using laravel web.php
) and given throttle 120 per second
.
everything was working properly but suddenly got following error while parsing using guzzle
{
"error": "FatalErrorException",
"reason": "Allowed memory size of 536870912 bytes exhausted (tried to allocate 266342400 bytes)",
"code": 1,
"trace": []
}
Using XAMPP server
and memory_limit=2048M
. If I access API in the browser it loads fine
Guzzle parsing code below
$client = new Client([
'base_uri' => env('API_URL'),
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'],
'debug' => true,
]);
Please, someone, help me how I can fix it? even I cleared cache also generated a new key
You have a memory leak on your script that always became from redefining a guzzle in a while loop
, try using memory_get_usage
to debug the memory usage of your script .
And don't define a class in loops , in your situation that you must use a adapter outside of the loops , like this :
$adapter = new \GuzzleHttp\Adapter\Curl\MultiAdapter(
new \GuzzleHttp\Message\MessageFactory()
);
while (true) {
$client = new GuzzleHttp\Client(['adapter' => $adapter]);
$client->get('http://example.com/guzzle-server');
echo memory_get_usage() . "\n";
usleep(20000);
}
EDIT :
You can debug more on memory eaters :
Memprof is a php extension that helps finding those memory-eaters snippets, specially in object-oriented codes. [SOURCE]
EDIT2 :
As the new comments , try to see memory usage via memory_get_usage
function before & after calling your api .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.