How to force PHP to release memory after script is finished
memory, mysql, php
Solution
When script is completed all resources should be freed, so you should not be in need of doing anything extra unless you use PHP/lib that seems to be buggy and leaks memory
EDIT
Memory leak is result of a bug and in languages like PHP if memory leak happens in PHP or its modules you will not be usually able to fix it other way by updating PHP or used modules to never version and hope this will come with leak fixed. You may try to narrow down what component is leaking and then report to its authors. As temporary solution you can work this issue around by restarting your httpd periodically which shall free all memory allocated by php or modules. But that's quick workaround only
Problem
I have a resource intensive script that takes about 30 minutes to complete. As its running it gradually consumes RAM. Once its finished it has consumed about 300mb. I need to run this script multiple times so this is becomming an issue. I have to restart the server every time i want to start running the script again as ram usage hits 100% and the script stalls. How can i force php or mysql to release the memory? PHP Version 5.4.29 Centos VPS 2GB ram Max memory for a php scripts is set to 512mb. The script makes a large amount of SQL queries. Should be in the 1000s. It also makes calls to 5 different APIs 50 times. It selects 50 row from a db table and loops through it, making calls to APIs etc to update it. Garbage collection is enabled var_dump(gc_enabled()); returned true ``` total used free shared buffers cached Mem: 2048 1522 525 0 0 182 -/+ buffers/cache: 1339 708 Swap: 128 0 128 ```