Memory Usage

My dev cms currently shows:

Execution Time: 0.231 sec | Memory Usage: 7.02 mb

Should i be worried about ?

I don’t have any opcode cache (xcache/apc) and i am running on a windows box on a stock wamp environment.

Is 7 MB to much memory usage ? What’s the limit where i need to worry about the memory usage ?

Depends on how many visitors your site will have… A memory limit for PHP scripts usually is much higher than 7MB (from 32 to 128MB), but it should not relax you ;)

Thanks for the reply.

Yes i know that the memory limit is higher on PHP, but you know, i was wondering if 7 MB is a high value for Yii.

I know that i am working on a 32 bit system on windows, and when i will migrate to linux the memory usage will drop with at least 1 mb (you know,linux/optimized host/64 bit S.O).

Now, going back to my app memory usage, the page that does this, has a CGrid View and instantiates several models, basically it just does a listing of the database records (10 rows if you’re interested)

But it loads and checks a lot of stuff before reaching the controller(about 10 additional components are loaded, including cache and database rbac + theme support).

The default Yii implementation shows:

Execution Time: 0.069 sec | Memory Usage: 2.71 mb

So i am wondering, why is there so much difference? could be the fact that i am using AR intensively ?

I don’t believe i coded so badly so that the memory usage is my bad, i believe this is because the components, but i cannot drop neither of them.

Maybe you, or anyone that sees this thread can post the output of:

Execution Time: <?php echo round(CLogger::getExecutionTime(),3);?> sec

Memory Usage: <?php echo round(CLogger::getMemoryUsage()/1048576,2);?> mb

And explain a bit which components are loaded, would be very helpful, not only for me .

Hmm, just a quick note, the default Yii login page, when it is loaded it outputs:

Execution Time: 0.095 sec | Memory Usage: 3.36 mb

and the contact page:

Execution Time: 0.095 sec | Memory Usage: 3.45 mb

So there’s some memory usage going out when a model is being used and a form is being generated and prepared.

too bad that nobody is interested in such topic, this should raise some question marks .

It’s not a secret Active Record eats a lot of memory (well, when you deal with a big amount of data), but only you can answer your question. If you have a high loaded site or very limited memory size, then you should avoid use of AR and use DAO instead. But if you choose AR, then be ready to pay for comfort :)

We’ve eliminated most critical memory leaks in AR around 1.1.2 so now AR is OK for not so large amounts of data. For large amounts of data you should consider using query builder.

It is not about using AR for comfort rather than being forced to do it many times to be on the same page with Yii itself(so that you can use the goodies Yii offers).

Other than that, if the website is a small one, then it doesn’t matter what you use, because you won’t have enough clients to kill it with multiple requests, if the website is medium/huge then anyway you need to go for a dedicated server, and there you decide what you install so you can put xcache and you are safe with using AR.

Now back to the issue, as you say, i expect to have a high memory usage with AR when i have many records, but with 10 records in the table? I mean, the schema is cached anyway, so we remain at AR and child classes usage. I know there was a bug in 1.0.x with memory not being released in AR instances, but i guess this is fixed now.

Another thing, let’s say i drop AR and use Query Builder/DAO but i still need to instantiate models for data validation for example, and as i pointed above, using models (either AR or Form) will consume a high amount of memory.

I remember that Yii dev team helped the people from STAY.COM to build the site, and they said they used only AR.

Well, that’s a high traffic website as far as i can see, so i am wondering what are the stats regarding memory usage there.

If you turn off logging and debug AR is not that memory consuming. At least when there are 10—30 records. This amount of records is pretty typical for most websites. My blog consumes about 4.2 Mb for front page where there are about 10 post records, some tags, nested views etc.

As for data validation, you’ll never use 10 models at once for data validation, right? Typically only one model is added/edited so AR is really suitable for this task giving a slight advantage with data validation, behaviors, scenarios etc.

There are no public stats about but you can try asking it here:

There are memory usage stats for that’s pretty large:

Thanks samdark, for such good answers.

Actually i believe i can go like this:

In the admin panel i can use all the goodies because, let’s face it, how crowded can be the admin panel, right ?

In frontend, i can go also with AR and if the things get too slow, i can write the queries with the query builder and still use AR for inserting/editing records , because in most of the time, a user makes "read" activities.

The thing is that using the AR gives so many advantages and not using it makes you do too much work and lose time (that’s the reason i left codeigniter), and time is too precious to be wasted.

You really shouldn’t worry too much about memory usage unless your site has very high traffic.

The yii project site (developed in Yii and using AR everywhere) has more than 70K page views per day, and it is running on a machine with 1.8GB memory.

Our test shows the site can accommodate at least 10 times more traffic with the current hardware configuration.

When your site has very high traffic, your first step is to adopt various caching techniques instead of changing AR to use DAO.

With proper caching (such as fragment caching, data caching, HTTP cache), you rarely need to create hundreds of AR objects for every of the concurrent user requests.

Qiang, thank you for sharing this with me and other people that will read this topic.

I tend to be worried, because i want to avoid these kind of issues from the start, but what you say gives me a lot of hope and i trust you are right.