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 }
source share