dotancohen
dotancohen

Reputation: 31471

PHP performance: Memory-intensive variable

Supposing a multidimensional associative array that, when printed as text with print_r(), creates a 470 KiB file. Is it reasonable to assume that the variable in question takes up half a MiB of server memory per instance if it is different for each user? Therefore if 1000 users hit the server at the same time almost half a GiB of memory will be consumed?

Thanks.

Upvotes: 0

Views: 264

Answers (1)

nico gawenda
nico gawenda

Reputation: 3748

There is an excellent article on this topic at IBM: http://www.ibm.com/developerworks/opensource/library/os-php-v521/

UPDATE

The original page was taken down, for now the JP version is still there https://www.ibm.com/developerworks/jp/opensource/library/os-php-v521/

Basic takeaways form it are that you can use memory_get_usage() to check how much memory your script currently occupies:

// This is only an example, the numbers below will differ depending on your system
echo memory_get_usage () "\ n";. // 36640
$ A = str_repeat ( "Hello", 4242);
echo memory_get_usage () "\ n";. // 57960
unset ($ a);
echo memory_get_usage () "\ n";. // 36744

Also, you can check the peak memory usage of your script with memory_get_peak_usage().

As an answer to your questions: print_r() is a representation of data which is bloated with text and formatting. The occupied memory itself will be less than the number of characters of print_r(). How much depends on the data. You should check it like in the example above.

Whatever result you get, it will be for each user executing the script, so yes - if 1000 users are requesting it at the same time, you will need that memory.

Upvotes: 5

Related Questions