如何“释放”内存循环?
我有一个在共享托管环境上运行的脚本,我无法更改可用的 PHP 内存量。该脚本正在通过soap 使用Web 服务。我无法一次获取所有数据,否则会耗尽内存,因此我在 mysql 数据库本地缓存数据方面取得了一些成功,以便后续查询更快。
基本上,我不是查询 Web 服务 5 个月的数据,而是一次查询 1 个月,并将其存储在 mysql 表中并检索下个月等。这通常有效,但有时我仍然会耗尽内存。
我的基本代码逻辑是这样的:
- 使用soap连接到web服务;
- 连接到 mysql 数据库
- 查询 Web 服务并将结果存储在变量 $results 中;
- 将 $results 转储到 mysql 表中
- 对于每个月的数据重复步骤 3 和 4
在每次迭代中使用相同的变量,因此我假设来自 Web 服务的每批结果都会覆盖内存中的前一批结果?我尝试在迭代之间使用 unset($results) 但这没有做任何事情。我每次都会输出使用 memory_get_usage(true) 的内存,并且每次迭代使用的内存都会增加。
有什么想法可以解决这个内存泄漏吗?如果我不够清楚,请发表评论,我可以提供更多详细信息。谢谢!
***编辑
这里是一些代码(我使用 nusoap 而不是 php5 本机肥皂客户端,如果这有影响的话):
$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
}
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:
- connect to web service using soap;
- connect to mysql database
- query web service and store result in variable $results;
- dump $results into mysql table
- 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 client 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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决了。至少部分如此。使用 nusoap 存在内存泄漏。 Nusoap 将调试日志写入 $GLOBALS 变量。更改 nusoap.php 中的这一行释放了大量内存。
更改
为
我更喜欢只使用 php5 的本机肥皂客户端,但我得到了奇怪的结果,我认为这些结果特定于我尝试使用的 Web 服务。如果有人熟悉使用 php5 的 SOAP 客户端和 www.mindbodyonline.com 的 SOAP API,请告诉我。
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
to
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.
您是否尝试过 $startingDate 上的
unset()
和 $mysqlResults 上的mysql_free_result()
?此外,即使这不是这里的问题,
SELECT *
也会受到皱眉。编辑:也许也释放 SOAP 结果。从一些简单的东西开始,看看是否有帮助。
Have you tried
unset()
on $startingDate andmysql_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.