Tip of the day

Don’t access the magento session object while you’re hooking the controller_front_init_before event, as it’ll be set up incorrectly and give you a cookie error. You probably wanted to hook a pre or post dispatch event instead.

Magento and memory usage in bulk scripts

So I was writing a script to check the integrity of configurable products on a couple of sites – it appeared that the attributes on some products didn’t match the actual set of child products available, so you had weird results like dropdowns with 1 color available.

It worked okay on a site with 250 active configurable products, but ran out of memory quickly on a site with 1000. What was going on?

It turns out that this line:

$attributes = $product->getTypeInstance()->getConfigurableAttributesAsArray($product);

consumed 0.5-2MB of memory for each product – what’s happening is that the Configurable type singleton is storing the configurable attribute data on the $product that we pass into that method, in order to reuse it later. If we’re not planning on doing anything else with that product (eg it’s from a collection we’re looping through), this is just wasted.

To remove all the stuff the above method adds to the product,

$product->setData('_cache_instance_used_product_attribute_ids', false);
$product->setData('_cache_instance_used_product_attributes', false);
$product->setData('_cache_instance_used_attributes', false);
$product->setData('_cache_instance_configurable_attributes', false);
$product->setData('_cache_instance_product_ids', false);
$product->setData('_cache_instance_products', false);

worked.