What is the maximum amount an array can support in PHP?

6

In PHP I usually work a lot with frameworks. Generally, they even bring query results to a database in array .

In some cases, when the number of data in a table or a relationship reaches a very large level, if we do not use features like paging or an ajax lazy load, that error can happen.

Fatal error: Allowed memory size of 99999999 bytes exhausted 

I've heard of cases where too much of an array has generated this error, but this can vary from one configuration of php.ini ( memory_limit ) to another. However, there is usually always a default value for the memory limit - which in this case is 128M for versions equal to or higher than PHP 5.3.

Considering the 128M default, would it be possible to arrive at a calculation, to know more or less, how much data we could use in a array ?

Is there a recommended maximum size for me to allocate values in a array ?

    
asked by anonymous 24.02.2016 / 13:05

1 answer

9

The problem in this case is not the array . It is the total amount of memory. Contrary to popular belief, array does not include everything that might appear to be in it. Most likely several data that is in it are references, so there will only be one pointer in the array , but the referenced object is actually occupying memory. And this data can be variable. An array needs to have its elements with fixed size, so the pointer needs to be used to ensure, so normalizes the equal size for all of them elements and with that indirection the size variation goes to another pointed object .

It is possible to even know how many elements fit in the array , but this does not solve the problem described, has no relevance and is information that will not help.

If you are having problems then you have to change the way you manipulate this data. And knowing how many elements can be used in the array does not help at all. Even as this may change in each run or at least over the lifetime of the project.

You can even do a time calculation to see if it will fit in memory or not, but it does not compensate for the effort, you will probably have two jobs, one to check if it fits and another to put it into the memory. It is better to divide and conquer logo. If there is the potential to burst memory, do not let this happen by splitting the rendering.

    
24.02.2016 / 13:29