How can I print out large variables in hook_entity_presave for debugging? I'm trying to print out the $entity variable im hook_entity_presave because I want to see all of the properties and their values at the time of saving.

The problem is that every way I have tried to print out the information, I get out of memory limit PHP errors.  My PHP settings are set to 1024M max memory.

I have tried print_r(), var_dump() and debug(), all with the same result.  I also tried dpm(), which allows the page to continue without running out of memory, but nothing gets output.

function my_module_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
  debug($entity, NULL, TRUE);
}
You don't want to see all properties. That's internal data that doesn't help you much anyway. What you want to see is the field values. And the easiest way to do that is this:

debug($entity-&gt;toArray());


That gives you a simple structure of all fields and their values. You can access them in a similar way as that array looks like, e.g. $entity->field[$delta]->property, [$delta] is optional and defaults to 0. See http://wizzlern.nl/drupal/drupal-8-entity-cheat-sheet for more details.

The other way is to use an IDE with a debugger, then you can set break points, inspect anything at runtime and even execute code.