Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Allowed memory size of 536870912 bytes exhausted (tried to allocate 27267072 bytes)

I inserted 100,000 rows to the table for testing. Now I simply want to echo that data to the blade file:

// Controller:
$data = Model::get();
return view('data.index', ['data' => $data]);
// index.blade.php:
{{ $data }}

And I get this error:

Allowed memory size of 536870912 bytes exhausted (tried to allocate 27267072 bytes)

The allowed memory is bigger than what it tried to allocate, so why it was exhausted? It's in my test environment so I am using XAMPP

0 likes
14 replies
Sinnbeck's avatar

Don't try to show 100000 records on a page. Use pagination instead

1 like
Ligonsker's avatar

@Sinnbeck I know, but for the testing I want to see some stuff and behavior (just for learning), realistically I am not going to do it.

I increased the allowed memory from 512M to 1024M in the php.ini and it worked. But still, why? If it said it tried to allocate much less than the limit of 512M.

Also, when checking the size of resources in the devtools, the 100,000 rows are 74.0 MB, much less than 512M

Sinnbeck's avatar

@Ligonsker using memory in php isn't the same as the html size. This was the point in that other thread

You can try changing it back to 512 mb and do this

$data = Model::get();
return 'foo';

And then change it to 1024 and see that the data is almost nothing

1 like
Ligonsker's avatar

@Sinnbeck Oh true, I just thought that since in this case I actually dumped the entire $data to the HTML it could be close (because I output everything in the blade using {{ $data }})

So doing what you suggested work, but I wanted to actually display the data in the HTML page

Ligonsker's avatar

@Sinnbeck Oh yes, I actually tried it after learning that in the previous post 😀

I just wonder then why it gives me that PHP error if the numbers don't seem to match?

Sinnbeck's avatar

@Ligonsker because they are wildly different things.

Php : get 100.000 records and load all data for every row into memory as php classes, keeping track of types and ordering of every single item

Html : just a string with a lot of characters

1 like
Ligonsker's avatar

@Sinnbeck So where does the 27267072 bytes come from? From the string size that it tries to dump to the HTML, but the actual data size is much bigger? Also, I tried to do this:

dd(mb_strlen(serialize((array)$data), '8bit'));

and it shows 188384164 (Bytes? so 188 MB?) And MySQL WorkBench shows only 16MiB,

So everything shows different size, that's why I don't understand which is the actual data size. Ok I can understand that PHP tries to allocate probably more than the actual data size, but the numbers are way off? And changing all the time

Sinnbeck's avatar

@Ligonsker from this

$data = Model::get();

The error is php (the server) trying to load that into the servers memory

And the conversion to html might also take alot of memory

1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Let me give an example that might help your understanding.

Imagine that you wrote a python script that went through every single file on your computer, and checked the path, name, file size and file type and stored these values in foo.txt. It opens all files to check them for errors no matter the size. It also keeps track of all files it has opened so far. Now this script might use 600 mb of memory (ram) as it needs to remember every single thing. But the resulting foo.txt might only be 10mb. See the difference?

Of course php doesn't do this exact thing. It's just an e example

1 like
Ligonsker's avatar

@Sinnbeck Oh yes. now I understand better. Also your previous comment made sense:

And the conversion to html might also take a lot of memory

Because when I did:

 $data = Data::get();        
 return 'foo';

I did not get the error even when the php.ini was set to 512M, but only when I output it to HTML it gave me this error. So I was wondering why.

And looking at the error more carefully, it looks like it exactly is the reason, it fails here:

return json_encode($this->jsonSerialize(), $options);

Thanks again :D

Sinnbeck's avatar

@Ligonsker yeah php only clears memory when it's completely done using it, so each operation might take 300 mb

1 like
Ligonsker's avatar

@Sinnbeck awesome, will read about that. Though I've never dealt with PHP's generators/iterators but maybe it's time :)

Thank you once again!

Please or to participate in this conversation.