CodeIgniter Forums

Full Version: Memory management
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Team,

I am working on a CI project LAMP on Ubuntu.

I only have a dev site currently but am concerned about the memory being used by my site, and in particular whether I am sensibly closing ci sessions.

I have already asked the wizards about garbage collection (we aren't using it at the moment), but wondered if I could help myself by exiting ci sessions when appropriate.

However I am not confident I understand when a new instance is started and therefore, when I can terminate the preceding instance - is it simply on the rendering of a new page, or staring up a new controller? If this is the case am I safe to code an exit() after any redirect (for example)?

I accept my memory problem could be in another subsystem, but I want to make sure my php environment is not hanging onto unnecessary memory.

Can anyone provide me some best practice please?

Thanks, Paul
And it's a normal website, as in you go to browser, you type in URL and that's it?

In that case the memory is handled by single PHP process per request, and if your script doesn't run out of memory, when it's done, it'll automatically clear the memory, so you don't have to manage memory yourself like you might have to for native applications.

Said that, there are few simple things you can look at to improve the scripts memory usage.

PHP7 is a lot more memory efficient without any changes to your code, so if you are still running on PHP 5, it's worth getting your PHP version upgraded.

Also, if you autoload all your models / helpers / etc, even the ones that you hardly ever use, that will use up more memory.
Hi Pertti, thanks for that. We are currently running 5.6 but I can see going to 7 might be a good move.

Can I ask you what you mean by request please? For example, if in our code we direct to another controller and render a page accordingly is that equivalent to a new request (which I assume you mean is from the browser??). In this case are we still running the original php process or another one?

Hopefully you can see my confusion/ignorance!
(08-15-2018, 12:58 PM)PaulC Wrote: [ -> ]Hi Pertti, thanks for that. We are currently running 5.6 but I can see going to 7 might be a good move.

Yeah, PHP7 is sweet, gives you couple of nice new features, but without a change, your PHP code, in 99.9% of cases, runs faster and more efficiently. I don't remember running into any issues when we upgraded.

(08-15-2018, 12:58 PM)PaulC Wrote: [ -> ]Can I ask you what you mean by request please? For example, if in our code we direct to another controller and render a page accordingly is that equivalent to a new request (which I assume you mean is from the browser??). In this case are we still running the original php process or another one?

So yes, one request is when you type the URL in your browser, and browser then asks server to give it something, based on that URL.

If URL is pointing to a PHP script (either directly, with myfile.php in URL, or in-directly via .htaccess URL rewrite), web server knows to use PHP library to process the script and send back output of that script.

The way Apache and PHP work, by default you have one system process per request, the moment PHP script is done, that process is killed. Because that setup, A + PHP automatically make use of multi-core CPUs, so requests can be processed concurrently.

It's possible to do multi-threading with PHP, apparently, but by default single script with all it's includes or any cross model/controller linking and what not, will run inside single system process.

If you use "redirect" instruction in your script, PHP script sends special message back to browser instead of your normal HTML and stops, killing the original process, then browser goes - oh, I am suppose to load this other URL instead, and makes a second request with different URL.

(08-15-2018, 12:58 PM)PaulC Wrote: [ -> ]Hopefully you can see my confusion/ignorance!

Eh, all good, I've done this years for bread and butter, and I still don't know anything Big Grin

If memory is your problem, are you running your DB server on the same machine as your web server?

It could be that you have queries that are joining together multiple tables and loads of data, that could easily cause some memory issues as some of this data need to be hold in memory while processed by DB server, before it's returned to PHP.
Hmm, thanks once again. Your last point is interesting. Yes we are running Apache and Mysql 5.7 on same machine and I have already reviewed some of the db transactions because they are returning silly amounts of data from the DB, simply because of not selecting the columns that are needed. Is there a way of flushing the mysql buffers? I find the number of configurations options for mysql to be quite bewildering :-(

Appreciate your thoughts and time, Paul