GeoIP memory leak


(anush) #1

I’m seeing massive memory leaks from the GeoIP plugin. I was trying to generate some dummy data using the generateVisits.php script. I keep getting this:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 43453970 bytes) in C:\wamp\www\piwik\plugins\GeoIP\libs\geoip.inc on line 317

I’ve tried to increase the memory limit up to 512MB and also tried lowering $maxVisitors to 10 – but always get the same problem. When I unload the GeoIP plugin, everything seems to work just fine. I’ve seen another thread on this forum regarding GeoIP and memory – the solution there was to increase the memory limit, but that solution does not seem to be viable here.

Any suggestions? Has anyone ever tried generating dummy data with the GeoIP plugin active?


(anush) #2

Ok, I think I found the leak. The GeoIP plugin was re-initializing the mapping datafile each time instead of reusing the existing instance. A simple null check on the datafile handle and preventing re-initialization solved it.

plugins/GeoIP/GeoIP.php:

protected function initGeoIpDatabase()
{
    if(!$this->geoIpDb)
    {
        require_once PIWIK_INCLUDE_PATH .'/plugins/GeoIP/libs/geoipcity.inc';
        $geoIPDataFile = PIWIK_INCLUDE_PATH . '/plugins/GeoIP/libs/GeoLiteCity.dat';
        // backward compatibility, old instructions asked to rename to GeoIP.dat
        if(!is_file($geoIPDataFile))
        {
            $geoIPDataFile = PIWIK_INCLUDE_PATH . '/plugins/GeoIP/libs/GeoIP.dat';
        }
        if(!is_file($geoIPDataFile))
        {
            throw new Exception("GeoIp file 'piwik/plugins/GeoIP/libs/GeoLiteCity.dat' could not be found! 
                                Make sure you downloaded and extract the GeoIp city database as explained on http://dev.piwik.org/trac/ticket/45");
        }
        $this->geoIpDb = geoip_open($geoIPDataFile, GEOIP_MEMORY_CACHE);
    }
}