0

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

Advaith
  • 2,490
  • 3
  • 21
  • 36
scott
  • 3,112
  • 19
  • 52
  • 90

1 Answers1

1

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 .

Root
  • 2,269
  • 5
  • 29
  • 58
  • i have aded $client = new Client([ 'base_uri' => $url, 'headers' => ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'], /* 'headers' => [ 'Accept' => 'application/json', 'Content-Type' => 'application/json',]*/ 'debug' => true, ]); in trait – scott Jun 17 '18 at 16:47
  • even i have retrieve only one record .will it effect ? – scott Jun 17 '18 at 16:48
  • it maybe not from the one time calling , you can debug it more too , see edited answer – Root Jun 17 '18 at 16:52
  • thanks but for me for calling api one time also giving same error but it was working fine yesterday even i restarted apache and my system no use – scott Jun 17 '18 at 16:57
  • see the `memory_get_usage` before & after calling the api , to check that problem came from the api calling really ? – Root Jun 17 '18 at 16:59
  • ya sure i will check – scott Jun 17 '18 at 17:04