Hello everyone! Recently I’ve noticed that some developers don’t pay much attention to memory management in Zend framework. And here I’m not talking about Zend_Cache, but rather about object sizes. And if you’re thinking that I’m writing nonsenses, let me show you how to reduce memory usage by more than 100 times (in my example :D).
First of all, I’m going to use PHP’s built in function memory_get_usage() to get difference. Just look at this example, and you’ll know what I’m talking about:
public static function findById($collectionId,$show_all=false) {
$result = new Application_Model_Collection();
$query_result = $result->queryById($collectionId);
$var1 = memory_get_usage();
//HERE I TOOK MEMORY USAGE BEFORE POPULATING THE RESULT
$result->populateFromQuery($query_result);
$var2 = memory_get_usage();
//AND HERE AGAIN AFTER POPULATING
echo ($var2 - $var1);
//ECHO THE DIFFERENCE IN BYTES
echo ' ';
$clean_result = Application_Model_Collection_Object::parseCollection($result);
return $clean_result;
}
And another snippet:
public static function parseCollection(Application_Model_Collection $obj)
{
$return = new Application_Model_Collection_Object();
$var3 = memory_get_usage();
//AND AGAIN BEFORE CLEANING THE OBJECTS
$i = 0;
foreach($obj->columns as $column)
{
$return->columns[$i]->values = $column->values;
$return->columns[$i]->old_values = $column->old_values;
$i++;
}
$i = 0;
foreach($obj->data as $data)
{
$return->data[$i]->values = $data->values;
$return->data[$i]->old_values = $data->old_values;
$i++;
}
$var4 = memory_get_usage();
//AND AFTER
echo $var4 - $var3;
//ECHO THE OUTPUT
return $return;
}
Note: above examples are cleaned methods from my existing Zend project
For my ~60 records in collection I got this as output: 1854968 17644.
Now, if I’d divide those two, I’d get something like this: 1854968 / 17644 = 105.1331, and that’s how many times new collection is smaller than the original one. All I did here was that I stripped unnecessary data from objects (like link to the database), and returned that back to the application to work with it. Again, this is only for cca 60 objects in collection, and on this same project, I got collections wit over 1000 records. Simple math would say that application would need around 30 MB for a collection of that size, compared to less than 0.5 MB when its optimized.
Anyway, I don’t want to say that you’re wrong if you don’t do this, but rather that you’ll get a much faster application, for exchange in small effort of optimizing the collections.
That’s all from me for now, and I hope you learned something today.
Cheers!