pChart 2.x forum
http://wiki.pchart.net/forum/

pChart is leaking memory
http://wiki.pchart.net/forum/viewtopic.php?f=5&t=3027
Page 1 of 1

Author:  piwik [ Wed Apr 11, 2012 6:03 am ]
Post subject:  pChart is leaking memory

We use pChart extensively in free software http://Piwik.org

we have noticed that many users now experience "Fatal error: Allowed memory size of XX bytes exhausted"
(For example here is a user report http://forum.piwik.org/read.php?2,88040 )

This problem happens only when graphs are enabled and the report generation is trying to generate graphs using pchart.
Because we generate many graphs in the same http request (the same request will do MANY things including for example generating 20 graphs and fetching the output to include it in a PDF). because it all happens in the same request, memory that is not freed is then not available for next function calls and next graphs.

It would be great if pChart would free memory using unset() calls where necessary (to unset objects).
One way to test for memory that we use in Piwik is:

Code:


   /**
    * Print memory usage
    *
    * @param string $prefixString
    */
   static public function getMemoryUsage()
   {
      $memory = false;
      if(function_exists('xdebug_memory_usage'))
      {
         $memory = xdebug_memory_usage();
      }
      elseif(function_exists('memory_get_usage'))
      {
         $memory = memory_get_usage();
      }
      if($memory === false)
      {
         return "Memory usage function not found.";
      }
      $usage = number_format( round($memory / 1024 / 1024, 2), 2);
      return "$usage Mb";
   }


We also have a small class Timer you could reuse: http://dev.piwik.org/trac/browser/trunk/core/Timer.php

It directly outputs memory leaks eg.

Code:
$t = new Timer();
// do stuff here...
//..................

// this will display the memory that was leaked ("new memory allocated") since the timer was instanciated
echo $t->getMemoryLeak();


I know that it can be very tricky to ensure no memory leak in PHP, but this would really help when pushing pCharts with such an extreme use case :-)

Thank you & keep up the great work!!

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/