Memory management in Zend framework

Memory management in Zend framework © by rodolfoclix/sxc

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();
$var2 = memory_get_usage();
echo ($var2 - $var1);
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();
$i = 0;
foreach($obj->columns as $column)
$return->columns[$i]->values = $column->values;
$return->columns[$i]->old_values = $column->old_values;
$i = 0;
foreach($obj->data as $data)
$return->data[$i]->values = $data->values;
$return->data[$i]->old_values = $data->old_values;
$var4 = memory_get_usage();
echo $var4 - $var3;
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.


You made it all the way down here so you must have enjoyed this post! You may also like:

Using Redis cache backend and session storage in Magento Marko Martinovic
Marko Martinovic, | 43

Using Redis cache backend and session storage in Magento

Consuming Magento REST service using Zend_OAuth_Consumer Darko Goles
Darko Goles, | 45

Consuming Magento REST service using Zend_OAuth_Consumer

Unit testing with Zend framework: Setting up environment for the first test using Netbeans IDE for php Darko Goles
Darko Goles, | 1

Unit testing with Zend framework: Setting up environment for the first test using Netbeans IDE for php


  1. @Josip – yes and no.

    Yes, you can create a new collection method that will grab only necessary information, but what when all of those objects have to be instanced as a StdObj (application specific request)? You don’t have much of a choice then, so my final answer is that sometimes you can, and sometimes just can’t. But great point anyway.

    And another point with yes and no is that I do have a peak before cleaning it, but I don’t have to “peak it out” through the rest of application if I strip it. In practice (forget the numbers), I managed to speed up application to unrecognizable point this way.

    BTW, thanks for the comment!

  2. It is pretty amazing, reducing more then 100 times. But wait a second – what about CPU usage? What we have here is this (correct me if i’m wrong): pulling from db, collecting, then stripping unnecessary part to reduce memory usage. Also, you already reach memory peak before calling method parseCollection.

    So, the best is to create new collection method that will grab only necessary information. Or I’m wrong? 🙂

  3. @Vedran
    Sure I can, but I’ll have to ask you to use “Request a quote” form at the bottom of this page in order to do so.

    Thanks! xD

  4. @Mladen
    Could you please give me an estimate how long it will take to optimize simple Magento project since I’m on vacation and I cannot work any Magento on vacation and I’m so curious?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.