gpt4 book ai didi

php - Magento/Zend Framework 中的内存泄漏

转载 作者:可可西里 更新时间:2023-11-01 13:45:25 25 4
gpt4 key购买 nike

当运行这个简单的脚本时,我得到下面发布的输出。这让我觉得我的代码或 Zend Framework/Magento 堆栈中存在内存泄漏。迭代任何类型的 Magento 集合时会出现此问题。有什么我遗漏或做错了什么吗?

脚本:

$customersCollection = Mage::getModel('customer/customer')->getCollection();

foreach($customersCollection as $customer) {
$customer->load();
$customer = null;
echo memory_get_usage(). "\n";
}

输出:

102389104
102392920
...
110542528
110544744

最佳答案

您的问题是,当您可以通过集合查询加载必要的数据时,每次迭代都会发出相当昂贵的查询:

$collection = Mage::getResourceModel('customer/customer_collection')->addAttributeToSelect('*');

将执行相同的操作,但全部在一个查询中进行。这种方法的警告是,如果 customer_load_beforecustomer_load_after 事件有任何自定义事件观察器(这些没有核心观察器),则需要运行观察器手动为每个数据模型。

编辑:归功于 osonodoar用于发现不正确的类引用(customer/customer 与 customer/customer_collection)

关于php - Magento/Zend Framework 中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com