Question

I'm having a strange problem where the following single operation causes an out-of-memory problem (limit at 512MB) when the following conditions are met:

  1. $product is a configurable product with no image. (placeholder/fallback image is available)
  2. $product has no simple products associated.
  3. $this->helper...->resize(135) is assigned to a variable (no problem when simply echoed in HTML).

Following is the code:

$variable = $this->helper('catalog/image')->init($product, 'small_image')->resize(135)

I tried casting it as a string and that relieves the problem and memory usage is not abnormal. Below...

$variable = (string)$this->helper('catalog/image')->init($product, 'small_image')->resize(135)

Any idea what's happening?

Additional info:

nginx error.log shows PHP message: PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 501481472 bytes)... (*!*this seems very abnormal to me. It's trying to allocate ~500MB on just that one call?)

Placeholder image is available.

Was it helpful?

Solution

Memory errors can be tricky to track down, and are almost always system specific (that's a fancy way of saying "I don't have a specific solution for you, but here's some debugging tips).

Memory problems usually happen for one of two reasons.

  1. Repeated loading of something in a loop without clearing memory from the previous loop

  2. Loading something too large

Based on your error message

Allowed memory size of 536870912 bytes exhausted (tried to allocate 501481472 bytes).

It sounds like you have PHP's memory limit set to 536870912 bytes (512MB), and PHP ran into a problem when it attempted to allocate 501481472 bytes. That is, in a single operation, PHP attempted to set aside around 478MB of memory to hold the image helper object you attempted to put in a variable. (resize returns $this, meaning a copy of the image helper)

$variable = $this->helper('catalog/image')->init($product, 'small_image')->resize(135)

My guess is your base images are huge files, and that one the objects used by the image helper had that image loaded in memory. When cast that object as a string (which for an image helper returns the images URL, see its __toString method.

$variable = (string)$this->helper('catalog/image')->init($product, 'small_image')->resize(135)

PHP didn't need to keep a copy of this around.

That, however, is just a guess. I'd up the memory limit temporarily to 1024MB to see if that clears up the problem. If it doesn't, that means you have a runaway loop loading too many things. If it does clear the problem up, takes steps to up PHP's memory limit only when this operation needs to run.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top