How to decrease the memory usage in Codeigniter? - php

When I upload my project on the online hosting server, the entry processes usage goes to 100%. It results to website down.
Using
$this->output->enable_profiler(TRUE);
I got this result.
MEMORY USAGE
1,263,856 bytes
Even a single page takes 1.2 MB
How can I solve my this problem, Please Help

I don't believe so. Maybe just make sure you aren't loading things where they aren't actually needed (like autoload loads things for each request whether you utilize it or not). CI actually has one of the smallest memory footprints of any a framework. All frameworks take more overhead than straight PHP. Try benchmarking the same thing in another framework and it will most likely use more memory than CI does

Related

Where can possibly be a memory leak in php and how to investigate it?

I'm using Codeigniter framework (development version from github) for one of my projects. Project itself is not big just a few controllers and models and I have a memory leak. In 12 hours my rams constantly go up and i have to restart php5-fpm to clean them. Where should I start looking for memory leak? I mean is it loops or variables and what tools can I use for this to investigate?
A very old question, but for those facing this problem (since it is still a problem for some of us using CodeIgniter)... per the developer: By default, CodeIgniter keeps an array containing your history of queries.
Look into setting save_queries to false in your database configuration.
I had the same problem with a work project and this dramatically reduced our memory usage.
If you need to find the exact cause of your memory leak, I will suggest you to use a memory profiler like XHProf.
This is the best way to debug the php scripts and what is the cause of memory leak.
Look out for:
1) Cron jobs
2) Queries using '*', and
3) Queries that use tables without INDEXES.
4) Specially, long, unoptimized queries. They are queries with lots of subselects, for exemple.
5) Run those queries with an EXPLAIN statement, and find out which tables should be optimized. Read here how to use explain: Using explain
You should install something like New Relic on your server/app, it has a free tier mode that can help you locate long queries and processes.
Now, I'm assuming you there isnt' any major processing that you are doing and didn't tell us about, for example, long lists of image processing.
That could kill a beast of a machine if unoptimized.
Remember that long loops can also be a memory leak. If thats your case, you could experiment switching from foreach loops for for loops, for example.

PHP codeigniter Memory usage around 4mb

I made the mistake of getting into the habit of autoloading a bunch of libraries, models..etc, when I don't need to. It is too hard to trace down all the cases to make sure everything is available and not broken. I am estimating the autoloading is causing 1-2 MB of extra resources per script. The total memory usage for my script is around 4Mb. (I used the profiler and disabled autoloading and saw that it dropped 1-2mb)
Is this something to worry about? The server I am running this on has 1gb of ram and doesn't ever seem to be under heady load.
Is this a bad thing? Am I worrying too much?
Is always better to load what you needs.
But 4MB is a normal PHP app memory usage.
I read somewhere, in case of a php optimized app to worry when you exceed 9MB.
Memory usage become important when your server have to respond many requests. So the goal become to use less server ressource as possible to pay less.
Sorry for my poor english.

Yii App Reaches Memory Limit On Linux Server

I have a really strange problem and I dont know how to solve it. My application reaches memory limit from time to time and Internal server error 500 happens. I have limit of 570MB on shared hosting. I tried to debug my application and YiiDebug Toolbar shows that every page is consuming about 10-12MB of memory. I dont really get where is the problem. On my local wamp server, there are no problems.
Can anyone help me? At least tell me where to start looking for memory leaks because I dont see any obvious.
This is unpredictable problem, it does not happen on some particular request.
I already commented 'YII_DEBUG' line in index.php.
If you've got a lot of AR records, you might also look into the brand new CActiveDataProviderIterator that just landed on master. It's not part of a stable Yii release yet and there is little documentation (I'm helping work on that now actually). But could be a place where you're hitting some memory limits.
And are you using GiiX by any change? I've found it's fairly inefficient in some places, leading to the need to query more leanly ...
See the posting at - http://www.yiiframework.com/forum/index.php/topic/15647-memory-usage/
A bit outdated but the points are still relevant.
If you can, use some kind of caching software to compliment the Active Record system.
If you are using Active Record, then ensure that the amount of models being loaded is not too much.
Debugging takes additional memory - if you do not need it disable it.
If the problem still persists consider moving from active record to DAO, but this can be messy.
What version of Yii are you using? and what is the number of typical visitors on your site?

Do too many requires / includes slow down PHP

I am now writing a php framework. I am wondering whether it will slow down when php require/include or require_once/include_once too many files during a request?
Well of course it will. Doing anything too many times will cause a slow down.
On a more serious note though, IO operations that touch disk are very slow compared to anything that happens in memory. So often times, including files will be a major performance factor when using a large framework (just look at Zend Framework...).
However, there are typically ways to alleviate this such as APC and similar op code caches.
Sometimes programming approaches are also taken. For example, if I remember correctly, Doctrine 1 has the capability to bundle everything into 1 giant file as to have fewer IO calls.
If in doubt, do some indepth profiling of an application written with your framework and see if include/require/etc are one of the major slow points.
Yes, this will slow your application down. *_once calls are generally more expensive, since it must be checked whether that file has already been included. With a lot of includes, there is a lot of hard disk access and a lot of memory usage bundled. I've developed applications with the Zend Framework that include a total of 150 to 200 files at each request - you really can see the impact that has on the overall performance.
The more files you include will add to some load. However, if you have to choose between require and require_once, require_once / include_once take more load because a check will need to be done by the server to see if the same file has been included elsewhere. So if you could possibly avoid that, at least you could boost performance.
Unless you use cache libraries, everytime a request comes those files would be included again and again. Surely it would slow things down. Create a framework that only include-s what needs to be include-ed.

Clearing memory_get_peak_usage Cache

I'm trying to track down a memory leak in a PHP Program (Magento, if it matters). The basic problem seems to be a leak in some object/class that's growing over time. That is, the more information that gets logged to the database, the more memory certain application processes end up using. Magento's a highly abstract system, so it's not always clear what code is being run that's consuming so much memory. That's what I'm trying to track down.
I've been using memory_get_peak_usage at the end of the program bootstrap file to benchmark performance, and seen a steady growth from 250MB of peak use, to 310MB of peak use in about a week. I would like to use memory_get_peak_usage intermittently throughout the execution cycle to ask
What was the peak usage prior to this call? [later in the cycle] What was the peak usage prior to this new call?
The problem I'm running into is, once I call memory_get_peak_usage once, any future call returns the same value as the first call, even when I know the peak usage has changed. This leads me to believe that after memory_get_peak_usage is called once, PHP caches the result. I would like to uncache it to perform the testing outlined above.
Can I call memory_get_peak_usage multiple times?
Are there alternative to profiling the scenario I've described above. Some feature of xDebug maybe?
Can I call memory_get_peak_usage multiple times?
Not sure on that one.
Are there alternative to profiling the scenario I've described above. Some feature of xDebug maybe?
Have a look at the XDebug profile page. It's been awhile since I have profiled an app, but when I did I followed the write-up and worked great.

Categories