FastTrack
FastTrack

Reputation: 8980

PHP Memory Cleared when using exit()?

My web app makes a lot of POST and GET calls via AJAX to my PHP scripts. I've noticed my server's memory usage is reaching a pretty low level after calling my scripts when exporting to PDF/excel, searching, inserting/updating/deleting, etc.

None of my PHP scripts end with exit() or die(). Could this be causing my memory issue? If I were to add exit() or die() at the end of each script, would this clear the memory that was just used by that given script?

Upvotes: 0

Views: 1159

Answers (3)

gd1
gd1

Reputation: 11403

I don't think exit() or die() would make any difference.

Please ensure you free database resources (result sets, connections, etc...). Moreover, consider all the extensions you use and ensure none of them has known bugs regarding memory leaks, especially if you use exotic extensions.

If you are on Linux and your server is not swapping on disk even if the "free memory" is "low", this could be due to disk caching. In that case, everything is fine. See: linuxatemyram.com.
BTW, where do you get the information about the free memory?

Upvotes: 1

sharp12345
sharp12345

Reputation: 4500

This is not a PHP issue, its an Apache feature.

When a apache starts a new process to handle your request, it doesn't immediately dispose its resources after your request has been handled, instead, it stays available for any possible future requests.

If you use 'top' command, you will see multiple apache processes running.

Read about it here: http://abdussamad.com/archives/169-Apache-optimization:-KeepAlive-On-or-Off.html

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324600

exit and die are basically the same as reaching the end of the file. Destructors are run, ob_start levels are flushed and ended, and any shutdown functions are run.

At the end of any PHP process, however, the process ends just like any other program. No memory is left over.

Upvotes: 3

Related Questions