2012-07-20 74 views
0

我在Magento中创建了一个模块,它通过创建随机产品,订单和客户来复制大型商店。上面描述的数据创建在我的模型中是直接的PHP代码。尽管使用了PHP的unset()和Magento的clearInstance(),但我仍然遇到严重的内存管理问题。我已经评论了〜下面的10行代码,说明我如何释放内存失败。Magento批量订单,产品和客户创建

/** 
* 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模型后如何清理的任何提示?

回答

0

我会使用资源,而不是模型,它会更快,更少的内存昂贵。如果这还不够好,将其转换为原始的sql,EAV在插入记录时并不复杂。

另一种方式,我也想尝试是:http://ringsdorff.net/2009/07/23/guest-post-fix-for-memory-leaks-in-magento/ 但是,这不建议在生产,所以我会建立一个包含了这些desctructors定制的客户模型,只是使用的新模式在我的剧本把其他所有的(生产)运行在正常模式。