Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JPustkuchen/a5f1eaeb7058856b7ef087b028ffdfeb to your computer and use it in GitHub Desktop.
Save JPustkuchen/a5f1eaeb7058856b7ef087b028ffdfeb to your computer and use it in GitHub Desktop.
Drupal 8 kint set maxLevels in settings.php to prevent out of memory

Currently devel doesn't allow to override KINT configuration in a clean way. Hopefully this will be possible some day via setting in UI or drupal setting override in settings.php Until this happens you may use this dirty trick in settings.php to override the setting.

See issues:

Simply copy this into your settings.php and change the value accordingly (Kint default: 7)

Kint ^3:

// Change kint maxLevels setting:
include_once(DRUPAL_ROOT . '/modules/contrib/devel/kint/kint/Kint.class.php');
if(class_exists('Kint')){
  // Set the maxlevels to prevent out-of-memory. Currently there doesn't seem to be a cleaner way to set this:
  Kint::$maxLevels = 4;
}

Kint >=4:

// Change kint maxLevels setting:
if (class_exists('Kint')) {
  \Kint::$depth_limit = 5;
}
@JPustkuchen
Copy link
Author

I think a dynamic path (get devel module path from service) isn't possible at this very early point in bootstrap?

@leymannx
Copy link

There's also https://www.drupal.org/project/devel/issues/2405179 and this patch https://www.drupal.org/files/issues/2018-10-09/make-dumper-plugins-configurable-2405179-16.patch and then you could put $settings['kint_maxLevels'] = '4'; into your settings.php

@leymannx
Copy link

Ah now I see it's also you who already commented in that issue.

@leymannx
Copy link

leymannx commented Jun 29, 2020

Now with Devel 4.x and kint-php as dependency the include must be removed and maxLevels becomes max_depth. The new default is 6. So in settings(.local).php put:

// Change kint max_depth setting.
if (class_exists('Kint')) {
  // Set the max_depth to prevent out-of-memory.
  \Kint::$max_depth = 4;
}

@EugeneMeles
Copy link

and if you want to enable resource-hungry plugins like "ClassMethodsPlugin", "ClassStaticsPlugin" and "IteratorPlugin"
put next:

// Change kint max_depth setting.
if (class_exists('Kint')) {
  // Set the max_depth to prevent out-of-memory.
  \Kint::$max_depth = 4;
  // Set Kint plugins
  \Kint::$plugins = array_merge(\Kint::$plugins, [
  	'\\Kint\\Parser\\ClassMethodsPlugin',
  	'\\Kint\\Parser\\ClassStaticsPlugin',
  	'\\Kint\\Parser\\IteratorPlugin',
  ]);
}

@nop1984
Copy link

nop1984 commented Dec 6, 2021

comment just to say this snippet is valuable, coming here at least once in 2 months to remind the details
thanks :)

@radube
Copy link

radube commented Jan 15, 2022

Just wanted to comment that at least for kint 4.1.1 you need to use \Kint::$depth_limit instead of \Kint::$max_depth. I hope that helps somebody to save some time 🙂

@JPustkuchen
Copy link
Author

Thank you @radube, updated the snippet accordingly :)

@kendallsvcr
Copy link

Just in case someone isn't quite sure about how to use it when is having the memory error (running out of memory) now a days with Drupal 8/9 just go to your settings.php or maybe a settings.local.php (for those who use it) and place this:

// Change kint depth_limit setting.
if (class_exists('Kint')) {
  // Set the depth_limit to prevent out-of-memory.
  \Kint::$depth_limit = 4;
}

note that as @radube said, for kint 4 it suffered a change from $depth_limit to $max_depth (thanks mate!)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Si tienes el problema de usar kint pero el sitio se queda sin memoria, utiliza el codigo anterior, directamente en el archivo settings.php o tal vez settings.local.php

@djassie
Copy link

djassie commented Apr 13, 2023

wokring -

if (class_exists('Kint')) {
  // Set the max_depth to prevent out-of-memory.
  \Kint::$max_depth = 4;
}

@djdemonNet
Copy link

Kint ^5 has the same syntax as and Kint ^4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment