CodeIgniter Forums
CI Memory Usage - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: CI Memory Usage (/showthread.php?tid=4965)



CI Memory Usage - El Forum - 12-27-2007

[eluser]section31[/eluser]
I'm new to CI and I've never used a PHP framework before and I'm a little alarmed at how much memory CI uses.

Am I correct in assuming CI uses so much memory because it buffers all of it's output data.

In the Loader Library I read this
Code:
/*
* Buffer the output
*
* We buffer the output for two reasons:
* 1. Speed. You get a significant speed boost.
* 2. So that the final rendered template can be
* post-processed by the output class.  Why do we
* need post processing?  For one thing, in order to
* show the elapsed page load time.  Unless we
* can intercept the content right before it's sent to
* the browser and then stop the timer it won't be accurate.
*/

It doesn't seem like those 2 reasons justify the reason for throwing all the output into memory.

What can I do to reduce as much memory as possible aside from caching since sometimes it's not an option? Is there anyway I can disable this buffering?

My first simple CRUD script claims it's using 1.2MB...and it's just an example. I'm assuming site's that get a lot of traffic avoid using php frameworks?

Can someone please enlighten me?


CI Memory Usage - El Forum - 12-27-2007

[eluser]tonanbarbarian[/eluser]
1.2Mb of memory is nothing.
CI is in fact the lightest framework I have used
As an example I tried to create a site using CakePHP. It was pretty straight forward crud for some average tables and CakePHP 1.1 used 4+Mb to display the page, CakePHP 1.2 used 5+Mb

And coming from a background where I have been building apps for an open source CMS for the last 3 years I am used to the CMS using 3-5M of memory before my code is even executed.

And by the way once I converted the above mentioned app to CI it was 1.5Mb to do the same functionality.

If you use CI properly you can create apps that use minimal memory. I plan one day to write a big post about how I feel this should be done, but here are some quick pointers, the ultimate goal of which is to reduce the amount of PHP code that is loaded. (Loading code that is not executed is one of the largest causes of PHP memory usage)

1. use the autoload is seldom as possible. I only really autoload the URI helper because it has redirection as well as site_url, if I dont need one I invariably need the other.

2. do not autoload the database unless you really need to. If your site has lots of redirects in it why load the database connection to simply redirect. If however you are using a db session or you have an authentication system that uses the database then definately autoload.

3. Put code into libraries where possible. If you have a controller with 400 lines of code in it with only 4 methods then consider breaking the code into multiple libraries and using them. You may be able to get the controller down to 50 lines and have 4 libraries of 100 lines each.


But ultimately if you are uber concerned by memory usage and 1.2Mb is too much for you then do not use a framework, or build your own framework from scratch so that you know 100% what is being run at each stage of the process. I have looked into the code CI code and I cannot find anything that I feel is unnecessary that is being loaded automatically.


CI Memory Usage - El Forum - 12-27-2007

[eluser]section31[/eluser]
Thanks for the reply.

RE:
2. I agree...I was looking at the database library and I was also shocked that it used lazy connection loading. Shouldn't it only connect if it's going to make a query. At least that's what I've read.

What about buffering, don't you think there should be an option to disable it to save on memory?


CI Memory Usage - El Forum - 12-27-2007

[eluser]tonanbarbarian[/eluser]
I am not sure that the ability to disable buffering would save any memory, unless they implement it differently.
The output of a typical page is only a few tens of kbytes. The issue would be making sure that if the buffering is disabled that the code that handles the buffering is never loaded.

From my experience it is not the data that is in memory that is problem it is the code that is being loaded and processes.
For your page that is 1.2M in size, if you were to display that memory usage, and then immediately unset every variable in memory then display the memory usage again I would suggest you would free no more than 200 - 300K probably taking you back to 1M

Actually I just tried that on my site without much difference at all
Peak Memory usage: 1840612 (1.755M)
Memory Usage at end of execution: 1756940 (1.676M)
Unset all variables: 1756380 (1.675M)

As you can see even the difference between the peak memory usage and the unset is 84232 or 82K
So out of the 1.755M used to process my request only .08 was actual data in memory. Now I do not know how efficient unset is at reclaim data but these figures do suggest that most of that 1.7M of memory was used by php to load, parse and execute the script, not for storage of data.

I have also noticed in similar experiments and testing I have done, in particular when trying to minimise the memory used by CakePHP, that the less code loaded the less memory used, however there is not a direct correlation between the size of the scripts files and the amount of memory used but if you look at all of the files loaded in the above example their total file size is much less than 1.7M

My above example is loading 61 files for a total of 318754 bytes or 311K yet it uses 3 - 4 times that amount of memory to process the page.
Anyway just my observations... the less code you load the less memory you use.


CI Memory Usage - El Forum - 12-27-2007

[eluser]section31[/eluser]
Thanks for the observations. You've been very helpful.