Skip to content
Advertisement

Allowed Memory Size Fatal Error; Improve Script

I have the following function which returns a formatted table to me showing me the list of browsers that viewed the website. It is retrieved from the MySQL Database and the User Agent (I know not totally reliable but it’s good enough for this at this point in time).

However; this function (and another that does operating system from the same user agent maxes our the Allowed Memory Size and throws the PHP Fatal Error. I increased my Memory Limit to 100 MB and then checked the memory_get_peak_usage() and the two are running around 56 MB.

Is there anything I can do to this function (and the os one not included but very similar to this one) to improve the memory usage?

JavaScript

Advertisement

Answer

At first glance, it seems that the most memory is used in the first query you do, as you load all data in that table just to count the results later on.

So, the first improvement would be to let the database do the count, as you already did for the OSes:

JavaScript

The second thing you should consider would be to format a bit the code since it looks a bit messy to me.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement