Test Results on Memory Usage of Zend Framework and Doctrine with APC

October 10th, 2009 by rvdavid Leave a reply »

After investigating a recommendation to use Doctrine by a fellow blogger, Brian at Real of Zod, I have decided to run with Doctrine as my Domain Model in Zend Framework projects. The thing is, if I’m going to commit to this, I need to know that applications I build in the future with the Zend Framework while using Doctrine as an integral part of the Model layer will not take performance hits from things like memory usage.

With Doctrine doing a _lot_ of magic, I thought that this would be something that I wanted to see for myself.

4MB Memory to execute a simple Query?!?! Ffffff#$#!!!!

A quick google search took me to a Question posted on StackOverflow about Doctrine Memory Usage. The concerned OP was asking if he had a server misconfiguration or if this was normal for Doctrine to be using so much memory for a simple query. He posted a 4MB difference in Peak Memory Usage between the start of the request before the Doctrine Query was executed and after the Doctrine Query was executed. After reading that, I was a little nervous.

Use Opcode Caching to reduce Memory Usage.

There were a couple of answers and comments one of the answers came from Doctrine’s own romanb (I think?) specifying that you _need_ opcode caching if you’re going to be using something the size of Doctrine (and Zend Framework for that matter).

Similar to what the OP at StackOverflow did, I created a simple test which measures peak memory usage when using Doctrine from within the Zend Framework.

Be warned however, that this is not a real benchmark. All this is testing is what memory usage is using the simplest of simplest operations. In this case, I created a query which pulls one record from the Database.

Here are the details.

The Controller I used for this quick test:


class IndexController
extends Zend_Controller_Action
// with doctrine set up and everything
* @return unknown_type
public function memoryUsageTestAction()
$q = Doctrine_Query::create()
->from(‘Default_Model_User u’)
->where(‘u.ID = ?’, 1);

$result = $q->fetchOne();
echo $result['FirstName'] . ‘ ‘ .$result['LastName']. ‘<br />’;


* Function from: http://www.php.net/manual/en/function.memory-get-usage.php
* @return unknown_type
protected function _echo_memory_usage() {
$mem_usage = memory_get_usage(true);

if ($mem_usage < 1024)
echo $mem_usage.” bytes”;
elseif ($mem_usage < 1048576)
echo round($mem_usage/1024,2).” kilobytes”;
echo round($mem_usage/1048576,2).” megabytes”;
echo “<br/>”;

Result without APC:

10.25 megabytes
RV David
16.5 megabytes

Result with APC:

3 megabytes
RV David
4.25 megabytes

This is with the following APC configuration:

apc.shm_size = 30
apc.ttl = 7200
apc.user_ttl = 7200
apc.mmap_file_mask = /tmp/apc.XXXXXX

apc.shm_segments = 1
apc.optimization = 0
apc.num_files_hint = 1000

As you can see, there’s a big difference – chopped ZF Memory Peak Usage (my configuration anyway) by about 65%-70% and Peak Usage with a Doctrine call to the Database by about 75%.

if you enjoyed this post, make sure you subscribe to my RSS feed!
You can also follow me on Twitter here.

Related posts:

  1. Zend Framework Model – Research into Possible Domain Model Solutions


  1. dev head says:

    Hi There,

    I’ve been working with zend and doctrine together for the past two years and of course I ran into the same issues.

    Doctrine is a memory monster, somethings that help.
    - APC ( like you said )

    - Increased memory ( of course )

    - Use PHP 5.3+

    - Compile Doctrine
    – doctrine can be compiled down to a single file. this helps reduce the overhead of the numerous includes. Documentation on that is on their site.

    - only fetch objects if you need to modify data, otherwise fetch the array.

    - use the query method and limit your query to only the models, relations and fields you need to have access to. ( writing a helper method to pass a config array can be a bit more handy then manually writing these out each time )

    Best of luck to ya.

  2. Andy says:

    Thank ya Captain Obvious!

  3. desfrenes says:

    Add this with long-lived CLI scripts and php cycle-reference and there you got my nightmare.

    • rvdavid says:

      I can just imagine… seems scary, but have you tried it at all?

      Zend Framework has a CLI interface so it can’t be that bad. No experience whatsoever with Zend Cli though.

      So yes, I’m just asking you to try it out without me knowing how well or badly it works. :P

  4. Brandon P says:

    I used to be an optimization junkie until I realized that renting hardware is much less expensive than writing/optimizing software.

    My choice to go with ZF + Doctrine was because it shortened development time, created a more managable & scalable application PLUS it makes programming ellegant & fun. In the beginning, I also realized the impact on the memory usage of my application.

    The amount of time I used to spend saving a few hundred milliseconds (or MB ram) here and there crippled development, resulting in a days of work with nothing to show. My application was eventually had the capability to handle 100,000 visitors a days… but I have yet to reach 1/1,000 of that.

    Any suggestion that I could give is “DONT PREMATURELY OPTIMIZE”. Your site doesn’t need to handle 10,000 concurrent users when you have 10.

    On your test results:

    Your test results yielded a 4MB increase. That’s roughly the size of 1 MP3 file. This *shouldn’t* be shocking, especially now when hardward is really cheap. If 4GB of memory usage is considered outrageous, you must think Firefox is the antichrist :)

    Our dedi-server has isntalled 4GB of ram with the capability to expand up to 16GB. If our application even started to test the limits of the server, which would be a better solution?:

    - Increase server memory to 8GB (Cost: $40/month, Time: 2 minutes)
    - Optimize & rewrite code (Cost: ???, Time: ???)

    If ANY of us are lucky to work on a project like Facebook, Twitter, etc then this conversation completely changes. But, since 99% of us won’t … don’t let a little memory usage deter you from using a great package like ZF + Doctrine to build your next web app.

    • rvdavid says:

      I can see where you’re coming from, but this post is not on prematurely optimising anything; or rewriting; or doing anything different aside from installing APC and configuring APC (which would take about 5 – 10 minutes) and seeing the improvements noted above.

      Why the test result from StackOverflow was ringing alarm bells was not due to the sheer size of it, but the amount of memory it took to process such a simple task for a single user.

      Thank you for taking the time in making a case against premature optimisation though. You certainly are passionate about it and with good reason. :)

      Keep in touch buddy.

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.