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);