开发者

How to "release" memory in loop?

开发者 https://www.devze.com 2023-04-04 08:58 出处:网络
I have a script that is running on a shared hosting environment where I can\'t change the available amount of PHP memory. The script is consuming a web service via soap. I can\'t get all my data at on

I have a script that is running on a shared hosting environment where I can't change the available amount of PHP memory. The script is consuming a web service via soap. I can't get all my data at once or else it runs out of memory so I have had some success with caching the data locally in a mysql database so that subsequent queries are faster.

Basically instead of querying the web service for 5 months of data I am querying it 1 month at a time and storing that in the mysql table and retrieving the next month etc. This usually works but I sometimes still run out of memory.

my basic code logic is like this:

  1. connect to web service using soap;
  2. connect to mysql database
  3. query web service and store result in variable $results;
  4. dump $results into mysql table
  5. repeat steps 3 and 4 for each month of data

the same variables are used in each iteration so I would assume that each batch of results from the web service would overwrite the previous in memory? I tried using unset($results) in between iterations but that didn't do anything. I am outputting the memory used with memory_get_usage(true) each time and with every iteration the memory used is increased.

Any ideas how I can fix this memory leak? If I wasn't clear enough leave a comment and I can provide more details. Thanks!

***EDIT

Here is some code (I am using nusoap not the php5 native soap c开发者_如何学编程lient if that makes a difference):

$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('Y-m-d', $startingDate)." AND date <= ".date('Y-m-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
}


Solved it. At least partially. There is a memory leak using nusoap. Nusoap writes a debug log to a $GLOBALS variable. Altering 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'd prefer to just use php5's native soap client but I'm getting strange results that I believe are specific to the webservice I am trying to consume. If anyone is familiar with using php5's soap client with www.mindbodyonline.com 's SOAP API let me know.


Have you tried unset() on $startingDate and mysql_free_result() for $mysqlResults?

Also SELECT * is frowned upon even if that's not the problem here.

EDIT: Also free the SOAP result too, perhaps. Some simple stuff to begin with to see if that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消