0

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.

1
  • WTF was “vim” tag doing here? Commented Apr 18, 2012 at 2:26

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

tl;dr: run memory_get_usage(), create your array, then run memory_get_usage() again, and subtract.
Looks like you also should pass true to memory_get_usage.
The JP version is still live, chrome translates it pretty good ibm.com/developerworks/jp/opensource/library/os-php-v521 I'll put the main information into the article

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.