Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Repro case:
- Copy links.html.twig to your theme
- 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
Comment #2
hass CreditAttribution: hass commentedComment #3
hass CreditAttribution: hass commentedComment #4
star-szrDump is just var_dump so yeah it will run out of memory. I recommend kint from devel instead.
Comment #5
hass CreditAttribution: hass commentedThat'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.
Comment #6
dawehner@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?Comment #7
hass CreditAttribution: hass commentedThe 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.
Comment #8
dawehner@hass
I won't answer your comment, due to your niceness.
Comment #9
hass CreditAttribution: hass commentedNo problem, i'm nice.
Comment #10
star-szr@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?
Comment #11
dawehnerExactly.
Comment #12
swentel CreditAttribution: swentel commentedRelated maybe: #1804998: Add LadyBug from Symfony to Core to allow debugging variables within templates
Comment #13
xjm@webchick, @alexpott, @catch, @effulgentsia and I agreed that this is a normal priority issue.
Comment #14
star-szrI'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.
Comment #16
FiNeX CreditAttribution: FiNeX as a volunteer commentedAnyway, even kint() returns the memory exhausted error when I've tried to debug the ECK twig template.
Comment #17
aliyakhan CreditAttribution: aliyakhan as a volunteer and commentedyes, even kint() does same. Any update on it?
Comment #19
joelpittetThere 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
Comment #20
hass CreditAttribution: hass commentedIt is still not clear how a few links can require 2gb+ of memory in a dump. That is not realistic.
Comment #21
dawehnerThe best "workaround" for that kind of problem is to simple use the right tool for the job aka. a debugger.
Comment #22
hass CreditAttribution: hass commentedIf 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.
Comment #23
swentel CreditAttribution: swentel commented@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.
Comment #24
dawehnerThere is also https://www.drupal.org/project/vardumper for it
Comment #25
yi_jiang CreditAttribution: yi_jiang commentedI 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
Comment #27
peezy CreditAttribution: peezy as a volunteer commentedI 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.Comment #28
yi_jiang CreditAttribution: yi_jiang commentedthx Peezy, it good to know.
Comment #29
hass CreditAttribution: hass commented@keezy: This is about Twig dump, not Krumo or Kint.
Comment #30
rbrownell@hass Depending on how deep you want to go, try something like kint(page|keys) or dump(page|keys).
Comment #31
peezy CreditAttribution: peezy as a volunteer commentedOops! 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.
Comment #32
littletiger CreditAttribution: littletiger commentedOr use vardumper() instead of dump() and kint(), with the twig_vardumper module.
Comment #33
Poindexterous CreditAttribution: Poindexterous commented@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.
Comment #35
Pranali.addweb CreditAttribution: Pranali.addweb at AddWeb Solution Pvt. Ltd. commentedI 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:
I Hope this solution helps you.
Comment #36
malaynayak CreditAttribution: malaynayak as a volunteer and at TA Digital commentedThis problem occurs because the array is self-referencing in nature. The solution is in a preprocess function:
Comment #38
AnybodyGist with a workaround until there is a setting in devel one day perhaps to override kint defaults cleanly:
https://gist.github.com/JPustkuchen/a5f1eaeb7058856b7ef087b028ffdfeb
Comment #42
sheanhoxieIf 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