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

Asked

Viewed 2,200 times

6

In PHP I often work a lot with frameworks. Generally, they bring the results of the query to a database in a array.

In some cases, when the number of data from a table or a relationship reaches a very large level, if we don’t use resources like paging or an ajax Lazy load, that error may happen.

Fatal error: Allowed memory size of 99999999 bytes exhausted 

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

Taking into account the pre-definition of 128M, it would be possible to arrive at a calculation, to know more or less, what is the amount of data that we could use in a array?

There is a recommended maximum size for me to allocate values in one array?

  • 3

    Wallace if an array reaches that amount something is very wrong in your code ;)

  • @Jorgeb. Yes, of course. Only with the Codeigniter to date, rsrsrsrs.

  • @Jorgeb. not always. It can be an input changed by the console and create something like: <input name="q[][][]" type="text" />, and input N indices by changing the attribute name with hundreds of [].

  • It’s up to the developer to work the data, I know, but it’s just one example that an array can be from an external source. By default abolishes array of forms, including checkbox’s.

  • 1

    @Papacharlie and, because it is an external form, you can "inject" a giant array to generate an error. I’ve already done it, with a suicide form.

  • @Papacharlie but here we are talking about arrays from the database, at least that’s what I understood.

  • @Jorgeb. array from the database was just an example. Actually, I did, because it’s one of the ways that it’s easier for you to generate an error from memory exhaustion. Other array sources are "accepted for the answer," but I can’t think of one that could impact as much as the database.

  • 1

    @Jorgeb, it was just an addendum

  • @Wallacemaxters, I’ve also manipulated mine in test environment and so I abolished array in form.

  • 1

    @Papacharlie but it doesn’t do much good. The guy who edit your form, can add a array there. I already managed to bug a system of a guy like ;0

  • @Wallacemaxters, your victim was probably wearing something like htmlspecialchars direct with the value of $_POST. In my case if the value is an array I do a casting for null value.

Show 6 more comments

1 answer

9


The problem in this case is not the array, is the total amount of memory. Contrary to popular belief, the array does not include everything that may appear to be on it. Most likely several data that are on 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. A array needs to have its elements with fixed size, so the pointer needs to be used to ensure, thus normalizes the equal size for all elements and with this indirect the size variation goes to another object pointed.

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

If you are in trouble then you have to change the way you manipulate this data and know how many elements can be used in array does not help at all. Including because this can change in each run or at least over the lifetime of the project.

It is even possible to make a calculation on time to know if it will fit in memory or not, but it is not worth the effort, probably will have two works, one to check if it fits and the other to put in memory in fact. It’s better divide and conquer logo. If there is the potential to burst the memory do not let this happen by parceling the processing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.