使用call()方法使用PHP SoapClient进行内存累积

时间:2016-05-30 22:23:45

标签: php memory-leaks soap-client

当我在循环中使用SoapClient类的call()方法时,它似乎在构建内存。

我试图在互联网上搜索解决方案,其中一些答案是禁用WSDL缓存,但它对我不起作用。 我尝试使用ini_set("soap.wsdl_cache_enabled", 0)禁用缓存并作为SoapClient类实例的参数。我试过了ini_set("soap.wsdl_cache_ttl", 0)。我也尝试过unset($this->_client)并取消设置($ this-> _session)。 没有什么对我有用。

ini_set("soap.wsdl_cache_enabled", WSDL_CACHE_NONE);


private function _call($method, $params = null)
{
    if (is_null($this->_client)) {
        $soapParams = array(
            'cache_wsdl' => WSDL_CACHE_NONE
        );
        $this->_client = new SoapClient($this->_getConfig('api/url'), $soapParams);
        $this->_session = $this->_client->login($this->_getConfig('api/user'), $this->_getConfig('api/key'));
    }

    $memoryBefore = memory_get_usage();
    echo "\n" . 'Memory Usage Before: ' . number_format($memoryBefore, null, '', '.');

    $result = $this->_client->call($this->_session, $method, $params);
    $memoryAfter = memory_get_usage();

    echo "\n" . 'Memory Usage After: ' . number_format($memoryAfter, null, '', '.');
    echo "\n" . 'Memory Usage Difference: ' . number_format($memoryAfter - $memoryBefore, null, '', '.') . "\n";

    return $result;
}

有什么方法可以解决这个问题吗?我做错了吗?

我正在通过命令行运行以下方法(使用PHP 5.3.29)

$ php -f datasync.php

Memory Usage Before: 13.244.400
Memory Usage After: 20.239.488
Memory Usage Difference: 6.995.088
Importing Orders...
1% [100/6055] - Page number (pagenum): 1 Page size (pagesize): 100 Total pages: 61
Memory Usage Before: 22.402.792
Memory Usage After: 29.519.432
Memory Usage Difference: 7.116.640
3% [200/6055] - Page number (pagenum): 2 Page size (pagesize): 100 Total pages: 61
Memory Usage Before: 29.519.984
Memory Usage After: 36.515.112
Memory Usage Difference: 6.995.128
4% [300/6055] - Page number (pagenum): 3 Page size (pagesize): 100 Total pages: 61
Memory Usage Before: 36.515.680
Memory Usage After: 43.723.440
Memory Usage Difference: 7.207.760
6% [400/6055] - Page number (pagenum): 4 Page size (pagesize): 100 Total pages: 61
Memory Usage Before: 43.724.056
Memory Usage After: 50.879.592
Memory Usage Difference: 7.155.536
8% [500/6055] - Page number (pagenum): 5 Page size (pagesize): 100 Total pages: 61
Memory Usage Before: 50.880.096
Memory Usage After: 58.083.616
Memory Usage Difference: 7.203.520
9% [600/6055] - Page number (pagenum): 6 Page size (pagesize): 100 Total pages: 61
Memory Usage Before: 58.084.232
Memory Usage After: 65.167.136
Memory Usage Difference: 7.082.904
11% [700/6055] - Page number (pagenum): 7 Page size (pagesize): 100 Total pages: 61    

1 个答案:

答案 0 :(得分:1)

问题不然了。对不起。 我试图隔离所有内容以找出代码占用内存的位置,但我忘记了递归执行的函数。

解决方案是用do-while替换递归方法。

请参阅:PHP: Memory leak in recursive function

我正在使用它:

    foreach ($result['items'] as $data) {
        $datasyncOrder->saveOrder($data, $this->_useOriginalIds);
        $this->_clv->printProgress($pageNumber, $result['pagesize'], $result['pages'], $result['page'], $result['total'], $index);
        $index++;
    }
    if ($result['page'] < $result['pages']) {
        $newResult = $this->_call('datasync.orders_export', array($result['page'] + 1, $result['pagesize']));
        $this->_importOrders($newResult, $pageNumber, $index);
    }

我换成了:

    $originalPageNumber = $pageNumber;
    do {
        try {
            if ($originalPageNumber == $pageNumber) {
                $this->_clv->printString(Mage::helper('datasync')->__('Loading Orders...') . "\n");
            }
            $result = $this->_call('datasync.orders_export', array($pageNumber, $pageSize));
            if ($originalPageNumber == $pageNumber) {
                $this->_clv->printString(Mage::helper('datasync')->__('Importing Orders...') . "\n");
            }
            $index = 1;
            foreach ($result['items'] as $data) {
                $datasyncOrder->saveOrder($data, $this->_useOriginalIds);
                $this->_clv->printProgress($pageNumber, $result['pagesize'], $result['pages'], $result['page'], $result['total'], $index);
                $index++;
            }
            $pageNumber++;
        } catch (Exception $e) {
            $this->_setOriginalEntityIncrementInfo('order', Mage::getModel('eav/config')->getEntityType('order')->getEntityTypeId(), $this->_storeId);
        }
    } while ($result['page'] < $result['pages']);