How to "free" memory in a loop?

I have a script that runs in a shared hosting environment where I cannot change the amount of PHP memory available. The script consumes the web service through soap. I cannot immediately get all my data, otherwise the memory shortage will end, so I got some success with caching data locally in the mysql database so that subsequent queries are faster.

Basically, instead of requesting a web service for 5 months of data, I request it 1 month at a time and save it in the mysql table and get the next month, etc. This usually works, but sometimes I still run out of memory.

my basic code logic is this:

  • connect to a web service using soap;
  • connect to mysql database
  • request a web service and save the result in $ results variables;
  • dump $ results to mysql table
  • Repeat steps 3 and 4 for each month of data.

the same variables are used at each iteration, so I assume that every batch of results from the web service will overwrite the previous ones in memory? I tried using unset ($ results) between iterations, but did nothing. Each time I output the memory used with memory_name (true), and with each iteration, the used memory increases.

Any ideas how I can fix this memory leak? If I was not clear enough, leave a comment and I can provide more details. Thanks!

*** EDIT

Here is some code (I do not use nusoap for the native php5 soap client, if that matters):

$startingDate = strtotime("3/1/2011"); $endingDate = strtotime("7/31/2011"); // connect to database mysql_connect("dbhost.com", "dbusername" "dbpassword"); mysql_select_db("dbname"); // configure nusoap $serverpath ='http://path.to/wsdl'; $client = new nusoap_client($serverpath); // cache soap results locally while($startingDate<=$endingDate) { $sql = "SELECT * FROM table WHERE date >= ".date('Ym-d', $startingDate)." AND date <= ".date('Ym-d', strtotime($startingDate.' +1 month')); $soapResult = $client->call('SelectData', $sql); foreach($soapResult['SelectDateResult']['Result']['Row'] as $row) { foreach($row as &$data) { $data = mysql_real_escape_string($data); } $sql = "INSERT INTO table VALUES('".$row['dataOne']."', '".$row['dataTwo']."', '".$row['dataThree'].")"; $mysqlResults = mysql_query($sql); } $startingDate = strtotime($startingDate." +1 month"); echo memory_get_usage(true); // MEMORY INCREASES EACH ITERATION } 
+4
source share
2 answers

I decided. At least in part. There is a memory leak using nusoap. Nusoap writes the debug log to the $ GLOBALS variable. Changing this line in nusoap.php freed up a lot of memory.

change

 $GLOBALS['_transient']['static']['nusoap_base']->globalDebugLevel = 9; 

to

 $GLOBALS['_transient']['static']['nusoap_base']->globalDebugLevel = 0; 

I would rather just use the php5 native soap client, but I get weird results that I think are specific to the web service I'm trying to use. If anyone is familiar with using the php5 soap client with the SOAP API www.mindbodyonline.com let me know.

+5
source

Have you tried unset() in $ startDate and mysql_free_result() for $ mysqlResults?

Also SELECT * disapproving, even if it is not a problem.

EDIT: It may also be possible to get a SOAP result. Some simple things to get started to find out if this helps.

-1
source

All Articles