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

```