1

I'm getting a fatal error when wrapping a loop with {% cache %}{% endcache %} tags. The full error is:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in /craft/app/helpers/StringHelper.php on line 473

Here's a small code sample of what I'm doing

{!--
  `craft.locationAPI.fetchAllLocations` grabs ~200 entries with related data via custom SQL queries.
  Doing this because craft.entries was throwing memory limit errors.
--}

{% cache %}
  {% for entry in craft.locationAPI.fetchAllLocations() %}
    ...
  {% endfor %}
{% endcache %}

I'm guessing this is because the amount of data getting returned (~13300 lines in "View Source") is too large for the encodeMb4 function in StringHelper.php.

Anything I can do to avoid this?

2
  • Pardon me asking the obvious here, but can you increase your php.ini file's memory_limit from 128M to 256M?
    – Brad Bell
    Commented Mar 3, 2016 at 1:31
  • That did the trick! I think that'll be fine for the development server, but I don't think we'll be able to set the production server quite this high. Prolly outta luck on this one? Commented Mar 3, 2016 at 1:56

1 Answer 1

1

Increase the amount of memory available to PHP through memory_limit is one option.

Another option is to reduce the amount of data (and therefore memory) you're dealing with in one request.

You don't share what craft.locationAPI.fetchAllLocations() is actually doing, but if you're returning results as Craft models, there is a significant overhead (performance and memory-wise) doing that with large data sets in Craft 2.x. You could look and return more primitive data structures.

Another alternative is to batch the results and retrieve them over multiple AJAX queries.

1
  • Thanks for the heads up, Brad. I think for this particular use case I'll not worry about caching because the amount of memory exhausted and queries used is fairly small. 21,304Kb / 18. I might use these options in other cases, however! Commented Mar 3, 2016 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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