How to force PHP to release memory after script completes

I have a resource intensive script that takes about 30 minutes to complete. As it starts up, it gradually consumes RAM. After its completion, it consumes about 300 MB. I need to run this script multiple times, so this is the problem. I have to restart the server every time I want to run the script again when the bar usage reaches 100% and the script stalls.

How do I get php or mysql to free memory? PHP version 5.4.29 Centos VPS 2GB RAM The maximum memory for php scripts is set to 512MB.

The script executes a large number of SQL queries. Must be in the 1000s. It also allows you to call 5 different APIs 50 times. It fetches 50 rows from the db table and goes through it making API calls etc. to update it.

Garbage disposal 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

      

+3


source to share


4 answers


When the script is finished, all resources should be freed, so you don't have to do anything unless you are using PHP / lib, which seems to be buggy and wasting memory

EDIT



A memory leak occurs due to a bug and in languages ​​like PHP, if a memory leak occurs in PHP or its modules, you usually cannot fix it in another way, by updating PHP or the modules used to the version and hoping that it will come with the leak fixed ... You can try to narrow down which component is leaking and then let the authors know. As a workaround, you can fix this problem by periodically reloading your httpd, which frees up all memory allocated by php or modules. But this is a quick workaround

+2


source


You can immediately set a variable to zero in free memory.

I think you need to adjust the database memory limit by adjusting the buffer size



How to set memory limit in my.cnf file

0


source


It is not PHP that uses this RAM.

Once the process is php

complete, it returns all of its RAM to the system. So it is not PHP that is calling this.

Certain other applications on the server take up additional memory, resulting in less availability php

when it starts. From what you've described, it's easy to imagine that this is MySQL. You said you are running 1000 requests. MySQL performs profiling and caching to optimize queries. This would completely cause it to take up extra memory after running the PHP script.

Also check other services. If any of the services are called from within your script, but continue to run between script executions, they may also take up more memory space. Any of these services can be configured for small chunks of memory, although some may not.

Of course, the solution runs PHP and MySQL on two different servers. It is much easier to configure each application to use their entire server than to configure them to share the same server. 2GB of RAM seems low for a script, which requires 300MB.

If your server resources are fixed, perhaps you can change the script to run longer, but also take up less memory. Perhaps you can load the biggest data into chunks and flush each result before loading the next. But for me it would only be a temporary solution.

0


source


Check the function ob_implicit_flush()

.

PHP docs

-1


source







All Articles