Last Updated: February 25, 2016
·
890
· daraff

Visualize the problem, when you have recursion issues

If you have a memory leak, sometimes its based on a recursive function which will be called again and again.
Usually you can use unit tests or the debugger to find the error. If you have a lot of recursive calls, it's sometimes difficult to debug the problem.

One possibility I see in PHP is to log some information and visualize the tree. If you have no information about the depth of the tree, you can use the PHP call stack in a strange way, but it works :D


<?php

$e = new Exception;

// calculate the stack depth
$depth = substr_count( $e->getTraceAsString(), "\n" );

// write some log entry into a logfile 
// and visualize the depth with str_repeat

file_put_contents('/var/log/visualize.log', str_repeat(".",$depth) . "my log message" . "\n", FILE_APPEND | LOCK_EX);