【问题标题】:Magento Mass Order, Product and Customer CreationMagento 批量订单、产品和客户创建
【发布时间】:2012-07-20 21:26:34
【问题描述】:

我在 Magento 中创建了一个模块,该模块通过创建随机产品、订单和客户来复制大型商店。上面描述的数据创建发生在我的模型中,它是直接的 PHP 代码。尽管使用了 PHP 的 unset() 和 Magento 的 clearInstance(),但我遇到了严重的内存管理问题。我在下面注释了大约 10 行代码,显示了我释放内存的尝试是如何失败的。

/**
 * Creates random customers.
 *
 * @param  int $offset, used to make sure the ids created are unique
 */
protected function _createCustomer($offset)
{
    // Create some random customer information
    $firstName = strtolower($_firstNames[array_rand($_firstNames, 1)]);
    $lastName  = strtolower($_lastNames[array_rand($_lastNames, 1)]);
    $customerEmail = $firstName[0] . $lastName.time().$offset."@weliketopumpit.com";

    // Retrieve customer model
    $customer = Mage::getModel('customer/customer')
        ->setWebsiteId(Mage::app()->getWebsite()->getId())
        ->loadByEmail($customerEmail);

    // Populate model and save it
    if(!$customer->getId()){
        $customer
            ->setEmail    ($customerEmail)
            ->setFirstname($firstName)
            ->setLastname ($lastName)
            ->setPassword ('password1')
            ->setCreatedAt(rand($this->_startDate, time()));
    }
    $customer->save();
    $customer->setConfirmation(null);
    $customer->save();

    $this->log(memory_get_usage() . "\n"); // Returns 17306840
    $customer->clearInstance();            // Called to clean up the object
    $this->log(memory_get_usage());        // Returns 17307304, memory wasn't unallocated

    debug_zval_dump($customer);            //Returns a refcount(2)
}

调用 unset 代替 clearInstance() 会产生相同的结果。使用 Magento 模型后如何清理的任何提示?

【问题讨论】:

    标签: php magento memory-management memory-leaks


    【解决方案1】:

    我会使用资源而不是模型,它会更快且内存成本更低。如果这还不够好,请将其转换为原始 sql,EAV 在插入记录时并没有那么复杂。

    我也会尝试的另一种方法是:http://ringsdorff.net/2009/07/23/guest-post-fix-for-memory-leaks-in-magento/ 但这不建议用于生产,因此我将构建一个包含这些析构函数的自定义 Customer 模型,并在我的脚本中使用新模型,让其他一切(生产)在正常模型上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多