Problem/Motivation

Repro case:

  1. Copy links.html.twig to your theme
  2. Add {{ dump(links) }} into the file
 * @see template_preprocess_links()
 */
#}
{{ dump(links) }}
{% if links -%}
  {%- if heading -%}
    {%- if heading.level -%}
      <{{ heading.level }}{{ heading.attributes }}>{{ heading.text }}</{{ heading.level }}>
    {%- else -%}
      <h2{{ heading.attributes }}>{{ heading.text }}</h2>
    {%- endif -%}
[09-Jan-2016 12:04:48 Europe/Berlin] PHP Fatal error:  Out of memory (allocated 312475648) (tried to allocate 294125568 bytes) in drupal8\vendor\twig\twig\lib\Twig\Extension\Debug.php on line 56

[09-Jan-2016 12:10:43 Europe/Berlin] PHP Fatal error:  Out of memory (allocated 415236096) (tried to allocate 398458880 bytes) in drupal8\vendor\twig\twig\lib\Twig\Extension\Debug.php on line 56

[09-Jan-2016 12:16:19 Europe/Berlin] PHP Fatal error:  Out of memory (allocated 415236096) (tried to allocate 398196736 bytes) in drupal8\vendor\twig\twig\lib\Twig\Extension\Debug.php on line 56

[09-Jan-2016 13:02:59 Europe/Berlin] PHP Fatal error:  Out of memory (allocated 159383552) (tried to allocate 142344192 bytes) in drupal8\vendor\twig\twig\lib\Twig\Extension\Debug.php on line 56

[09-Jan-2016 13:08:29 Europe/Berlin] PHP Fatal error:  Out of memory (allocated 414711808) (tried to allocate 398458880 bytes) in drupal8\vendor\twig\twig\lib\Twig\Extension\Debug.php on line 56

My development server memory limit per php.ini is memory_limit=2048M what should cover really everything.

Proposed resolution

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

hass created an issue. See original summary.

hass’s picture

Issue summary: View changes
hass’s picture

Issue summary: View changes
star-szr’s picture

Status: Active » Closed (works as designed)

Dump is just var_dump so yeah it will run out of memory. I recommend kint from devel instead.

hass’s picture

Status: Closed (works as designed) » Active

That's a joke, isn't it? A simple links array makes an out of memory? Why? Why exists such features that cannot dump a single string? I tried it in other places with strings, too.

Kint cannot used inside a twig template as I know. Using kint also causes troubles as the output is cached.

dawehner’s picture

@hass
Please be nice to people, as you want the same from them. Your assumption that kint() is not available from within templates is wrong, see http://cgit.drupalcode.org/devel/tree/kint/src/Twig/KintExtension.php.

@Cottser
What about also replacing dump() from within the kint module, just to have both things available?

hass’s picture

The question was why dump in twig is not working and causing out of memory. Pointing me to kint does not fix this bug. Closing this valid case also does not.

dawehner’s picture

@hass
I won't answer your comment, due to your niceness.

hass’s picture

No problem, i'm nice.

star-szr’s picture

@hass if you want dump "fixed" (as I said it's just var_dump so not sure what you're going to change) you're going to have to file an upstream bug with Twig.

@dawehner not sure what you mean by replacing dump - make dump do kint when kint is installed?

dawehner’s picture

@dawehner not sure what you mean by replacing dump - make dump do kint when kint is installed?

Exactly.

swentel’s picture

xjm’s picture

Priority: Major » Normal

@webchick, @alexpott, @catch, @effulgentsia and I agreed that this is a normal priority issue.

star-szr’s picture

Status: Active » Postponed (maintainer needs more info)

I'd rather not magically change dump() to kint() when kint is installed, it feels too magical. What if you have kint installed but still want to use dump() for some reason?

IMO this should still be closed won't fix like I said in #4. This is not a Drupal issue, it's a PHP issue if anything, not even Twig. I could be wrong but I'm fairly sure the exact same thing would happen if you had the same object(s) in PHPTemplate and ran var_dump() on them.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

FiNeX’s picture

Anyway, even kint() returns the memory exhausted error when I've tried to debug the ECK twig template.

aliyakhan’s picture

yes, even kint() does same. Any update on it?

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joelpittet’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

There has been documentation for this in the Theming guide.
https://www.drupal.org/docs/8/theming/twig/discovering-and-inspecting-va...

Feel free to re-open if you have a solution to deal with this, closing to triage. #14 needs addressing

hass’s picture

Status: Closed (won't fix) » Active

It is still not clear how a few links can require 2gb+ of memory in a dump. That is not realistic.

dawehner’s picture

The best "workaround" for that kind of problem is to simple use the right tool for the job aka. a debugger.

hass’s picture

If I'd like to dump a few variables this is the debugger and this should work and not crash PHP. This is a feature of Twig and it seems to be broken in Drupal.

swentel’s picture

@hass no - this isn't Drupal specific. Symfony has the same problem as well depending on what you dump. Ladybug is what most symfony people advise to use. There's even a Drupal extension at https://www.drupal.org/project/ld - although I haven't tested it yet.

dawehner’s picture

yi_jiang’s picture

I got the same issue, as a developer just wish to template a view tpl, it's very expensive to use kint or dump. site went to down after I try to dump the variables. I am not the frontend dev, so how is the fast way to see the variables

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

peezy’s picture

I believe the memory limit issue is related to recursions ( see https://www.drupal.org/node/2277303#comment-11168509 ). Krumo used to just print an infinity sign and move along; however, Kint goes deeper into arrays/objects which eventually maxes out the PHP memory limit.

The good news is that there is a request to add a UI to Kint to set the max-levels (see https://www.drupal.org/node/2405179 ). Until then a good workaround is to make sure your memory level is at 1024M and patch ~line 78 of /modules/contrib/devel/kint/kint/config.default.php so that it says $_kintSettings['maxLevels'] = 5;. The default level is 7.

yi_jiang’s picture

thx Peezy, it good to know.

hass’s picture

@keezy: This is about Twig dump, not Krumo or Kint.

rbrownell’s picture

@hass Depending on how deep you want to go, try something like kint(page|keys) or dump(page|keys).

peezy’s picture

Oops! Sorry, I got here by searching for my error and neglected to thoroughly review the issue description (insert face palm here).

This may work:

Because Twig uses the PHP var_dump function internally, try installing xdebug, which limits the var_dump() output of arrays and objects to 3 levels deep.

littletiger’s picture

Or use vardumper() instead of dump() and kint(), with the twig_vardumper module.

Poindexterous’s picture

@Peezy:

Thank you so much about pointing out the max levels setting for kint. You saved me a lot of headaches with that info as I don't have xdebug set up yet.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Pranali.addweb’s picture

I was facing the same issue.

We can't dump a large amout of data or a complete array in Drupal 8. This is an issue. But there is a solution as well, either we should know the exact key name or machine name of the variable or filed we want to print. Or we can print the keys existing the array. That could be helpful or us. For this we can use dump function syntax for dump is {{ dump(_context|keys ) }} or we should go for kint function which is useful to understand the elements with a good UI look of the dump. For this we enable devel module. Syntax for kint it {{ kint() }}. Here i am giving few dumping examples which can be usefull to come up with the problem of dumping.

Discovering all available variables in a template

To dump all available variables and their contents in a template, add the following to your template (after enabling debugging):

{{ dump() }} -->It could give a memory error if the amount of data is large.

To dump only the available variable keys use:

{{ dump(_content|keys) }} -->Play with this , get the keys and use them instead.

Beware of dump()

If you want to see all variables, but dump() results in exhausted memory because of recursion or the like, you can loop through _context to see all the keys in it:

<ol>
  {% for key, value in _context  %}
    <li>{{ key }}</li>
  {% endfor %}
</ol>  -->This could be also a solution.

I Hope this solution helps you.

malaynayak’s picture

This problem occurs because the array is self-referencing in nature. The solution is in a preprocess function:

$links = json_decode(json_encode($variables['links']));
//or
$links = unserialize(serialize($variables['links']));
var_dump($links);

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Anybody’s picture

Gist with a workaround until there is a setting in devel one day perhaps to override kint defaults cleanly:
https://gist.github.com/JPustkuchen/a5f1eaeb7058856b7ef087b028ffdfeb

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

sheanhoxie’s picture

If you have xdebug installed, you can override some of the settings in php.ini, to be a bit more memory friendly. Max Depth seems to be the biggest culprit, if I go above 8 I start hitting issues.

I've found success with the following on local environment with 1GB limit.

[xdebug]
xdebug.var_display_max_children = 128
xdebug.var_display_max_data = 1024
xdebug.var_display_max_depth = 8

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.