?

Log in

No account? Create an account

PHP memory usage (references)

« previous entry | next entry »
Oct. 9th, 2007 | 10:02 am
location: Singapore
mood: accomplishedaccomplished
music: Michael - Franz Ferdinand

Warning: PHP4 has a 16 bit reference count which severely limits how many entries you can put in an array using this technique. Your program will corrupt memory if you exceed this limit. This is fixed in php5 which has a 32 bit reference count.

I just discovered a cute way to save memory in php. One of my favorite data structures is an inverted array where the interesting values are the keys, and the array values are meaningless. For example:
array(
  0 => "bunny",
  1 => "paboom",
  2 => "mummy",
);

becomes
array(
  "bunny" => true,
  "paboom" => true,
  "mummy" => true,
);

The idea is that I can check for array values in constant time now. The loss of the indices is not important because the data was not ordered to begin with, even though it was represented as an ordered array. Basically it's a hash table.

The only problem here is that I'm storing a copy of true for every single array entery. What can we do about that?
$start_mem = memory_get_usage();
$arr2 = array();
$bool = true;
for ($i = 0; $i < 10000; $i++) {
  $arr2[$i] =& $bool;
}
$end_mem = memory_get_usage();
print "Used " . ($end_mem - $start_mem) . " bytes for 10000 references to \$bool with integer keys\n";

$start_mem = memory_get_usage();
$arr = array();
for ($i = 0; $i < 10000; $i++) {
  $arr[$i] = true;
}
$end_mem = memory_get_usage();
print "Used " . ($end_mem - $start_mem) . " bytes for 10000 copies of true with integer keys\n";

Used 465776 bytes for 10000 references to $bool with integer keys
Used 625600 bytes for 10000 copies of true with integer keys

There you have it. Instead of storing a copy of true for every array element, I simply use one copy of true and create many references to it. The memory savings are around 25%

Link | Leave a comment | Share

Comments {0}