Page 1 of 1

Array-element size

Posted: Mon Jun 27, 2005 12:22 pm
by tores
Hi

I'm going to store lots of db-entries in memory at my infrequently accessed web-server for short periods of time. In memory these entries are sited in an array.
Each element in this array consists of an integer key and three integer values (declared as int(11) in my mysql-db).
If each integer is 4 bytes, then each element should consume 16 bytes of memory. At a maximum I excpect to have about 4000 entries in the table. This should be about 64 KB of memory used. Am I right?

regards tores

Posted: Mon Jun 27, 2005 1:10 pm
by senthilnayagam
PHP default configuration allows 8 mb memory for a PHP script

content management systems generally take upto 2 mb

smarty around 300kb
pear apps 200+ kb
Yellow Duck 189kb


64kb is peanuts, dont waste your time worrying :))

regards
Senthilnayagam

Posted: Tue Jun 28, 2005 12:40 am
by Syranide
tores you are forgetting multiple thing, such as all variables in PHP is "variant" of type, meaning they are larger than what they really are, as they first off need more memory to tell what type they are, and secondly, the array type in PHP requires memory, I can guess it is a hashmap. so, I guess you have a number times the number of rows in the array.

A fast guess would be somewhere around 128-256kb
(I don't know if that counts as PHPmemory too, but, it is always good to know how it works)

Eitherway it shouldn't be any problem, but isn't this something you can do sequentially? Take each row as you get? Or must you really have everything before you can do something?

Posted: Tue Jun 28, 2005 1:58 am
by tores
Yes. I do this for performance reasons. Every element will be altered, so instead of having to read and write every entry sequentially to and from the db, much time may be saved by reading the whole bulk operate on it and then write the whole bulk back... However I have optained some code that measures the memory-usage of the current process.

Code: Select all

function getMemUsage()
{
  
  if (function_exists('memory_get_usage'))
    {
      return memory_get_usage();
    }
  else if ( strpos( strtolower($_ENV["OS"]), 'windows') !== false)
    {
      // Windows workaround
      $output = array();
      
      exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output); 
      return substr($output[5], strpos($output[5], ':') + 1);
    }
  else
    {
      return '<b style="color: red;">no value</b>';
    }
}
By doing some measurements using this function I found that by having an array where each element is built from four integers doesn't use critical amounts of memory. In fact an 10000-entry table with values 99999999999 (11 digits) uses about 6 mb of memory, and this is more than the double of the 4000-entries maximum I'm expecting to use.

regards tores

Posted: Tue Jun 28, 2005 2:01 am
by Syranide
btw, a good thing to keep in ming if you want to keep your memory cost low (much lower) is to simply cast all integers to integers, as PHP easily can end up storing them as strings (which they probably are when gotten from MySQL)...

So when creating the array, use (int) before assigning and you would most likely cut your memory usage in half or more.