2

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.

8
  • It might help (or might not) to see where the process is when PHP runs out of memory (i.e. copy/paste the exact error message)
    – Alan Storm
    Sep 17 '13 at 18:11
  • Hm, our nginx error.log shows only PHP message: PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 501481472 bytes).... Where can I find a more detailed log? Sep 17 '13 at 18:22
  • Do you have a fallback image in place? This can be set under System > Configuration > Catalog
    – Sander Mangel
    Sep 17 '13 at 18:47
  • @Sander, yeah it's available. When I cast string, it returns the placeholder image. Sep 17 '13 at 18:52
  • Is it a specific image that it crashes on? And if so what's the image file type?
    – Sander Mangel
    Sep 17 '13 at 18:54
5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.