Fatal error in StringHelper.php when using cache block around loop 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?
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.