Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP Fatal Error 'yii\base\ErrorException' with message 'Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)' #3

Closed
githubjeka opened this issue Dec 17, 2018 · 6 comments

Comments

@githubjeka
Copy link

in /var/www/html/vendor/halaxa/json-machine/src/Parser.php:181

$accounts =\JsonMachine\JsonMachine::fromFile('data.json');

FILE:
https://mega.nz/#!SUchmYSA
key is 8nEKU0-JUQzx39x8V1_my_dELb71C12rG5knMULEySc

@halaxa
Copy link
Owner

halaxa commented Dec 17, 2018

The provided JSON file contains subtree "accounts". Did you try to iterate that subtree or the whole file?

This might go out of memory:

$accounts = JsonMachine::fromFile('data.json');

because it iterates the top level and your code in foreach will be given $key == "accounts" and $value == [array of all accounts]

This will work: (tested on provided file)

$accounts = JsonMachine::fromFile('data.json', '/accounts');

because it will lazy iterate the huge array at "accounts" one by one (see Parsing a subtree).

@halaxa
Copy link
Owner

halaxa commented Jan 3, 2019

Closing as not a bug. Feel free to comment if needed.

@halaxa halaxa closed this as completed Jan 3, 2019
@Mwisho
Copy link

Mwisho commented Feb 27, 2019

Hi Halaxa, I'm getting the same error with this code:

require_once __DIR__ . '/vendor/autoload.php';
$client = new \GuzzleHttp\Client();
$respose = $client->request('GET', 'https://url/'. $sku .'/combinations', 
    ['headers' => [
        'API-Secret' => 'xxx',
        'User-ID' => 'xxx'
    ]
]);
// Gets PHP stream resource from Guzzle stream
$phpStream = \GuzzleHttp\Psr7\StreamWrapper::getResource($respose->getBody());
$result = iterator_to_array(\JsonMachine\JsonMachine::fromStream($phpStream));

The response looks like this:

[{"product":{"information":{"deliveryDays":"8","plano":{"width":206,"height":71}},"attributes":[{"attribute":"format","value":"330 Ml"},{"attribute":"bottle colour","value":"Transparent"},{"attribute":"type bottle cap","value":"Sports Cap"},{"attribute":"colour options","value":"Dark Blue"},{"attribute":"quantity","value":"96"},{"attribute":"printing process","value":"Digital"},{"attribute":"printing colors","value":"4\/0 Full Color"},{"attribute":"delivery time","value":"Normal"}],"price":10.10}},{"product":{"information":{"deliveryDays":"8","plano":{"width":206,"height":71}},"attributes":[{"attribute":"format","value":"330 Ml"},{"attribute":"bottle colour","value":"Transparent"},{"attribute":"type bottle cap","value":"Sports Cap"},{"attribute":"colour options","value":"Dark Blue"},{"attribute":"quantity","value":"144"},{"attribute":"printing process","value":"Digital"},{"attribute":"printing colors","value":"4\/0 Full Color"},{"attribute":"delivery time","value":"Normal"}],"price":10.10}}, ...

I tried $result = iterator_to_array(\JsonMachine\JsonMachine::fromStream($phpStream, '/')); but that way I only get the first item...

@halaxa
Copy link
Owner

halaxa commented Feb 28, 2019

Hi @Mwisho, I apologize for the possibly misleading guzzle example. The iterator_to_array call was there just for the test. You souhldn't use it in production as it loads everything into memory. The example has been fixed in master branch. You need to iterate the iterator to laverage the advantages of JsonMachine.

Try:

...
// Gets PHP stream resource from Guzzle stream
$phpStream = \GuzzleHttp\Psr7\StreamWrapper::getResource($respose->getBody());
foreach (\JsonMachine\JsonMachine::fromStream($phpStream) as $product) {
    var_dump($product['information']);
}

@Mwisho
Copy link

Mwisho commented Feb 28, 2019

Thanks for your fast response and the fix!

@halaxa
Copy link
Owner

halaxa commented Feb 28, 2019

@Mwisho, by the way, thank you for pointing out, that pointer '/' returns first item when iterating array. That's wrong behaviour. It should not return anything. I'm creating an issue for that #6 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants