TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Config_Database and the Out of Memory Error
  • The Problem

    I'm getting intermittent out of memory errors when I attach Config_Database to Kohana::$config. Most of my requests are using just shy of 6MB, but every so often (seemingly at random) I'll have a request hit the limit (128MB). All subsequent requests will continue to hit that limit until I comment out my line attaching Config_Database. Then, after one successful page load, I can uncomment the line and everything will be fine for a while again.

    As this usually results in the server closing the connection without sending any data, it's been a little difficult to discern exactly where in the process this is occurring, but with some trial and error, I think that it happens on the first config group load after attaching Config_Database.

    In the process of isolating this, I made sure to remove all modules except for database, and made a nearly-empty controller that loaded a config group and died. I was able to reproduce the error with this, so I doubt that it's anything in the code I have written. I assume this has to be an infinite loop to hit such a memory limit, but I have no idea how to find it.

    The OS is Ubuntu Server running the default LAMP install with Kohana 3.2 - nothing unusual here.

    Log Data

    From apache2/errors.log:

    [Sat Sep 03 20:39:28 2011] [error] [client] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in /home/matt/workspace/modules/database/classes/kohana/db.php on [line 63](https://github.com/kohana/database/blob/3.2/master/classes/kohana/db.php#L63)

    For the sake of science, I upped the memory limit to 256MB, the error then changed to this:

    [Sun Sep 04 12:01:28 2011] [error] [client] PHP Fatal error:  Allowed memory size of 268435456 bytes exhausted (tried to allocate 523800 bytes) in /home/matt/workspace/system/classes/kohana/config.php on [line 65](https://github.com/kohana/core/blob/3.2/master/classes/kohana/config.php#L65)

    Miscellaneous Notes

    Restarting apache does not resolve the problem. The only working solution is to comment out Kohana::$config->attach(new Config_Database), reload any page, and then uncomment the line.

    Once the error occurs, it will occur in additional browsers on page load, however, once the problem is resolved, each individual browser must do their own reload with the aforementioned line uncommented or they will continue to have the same issue.

    If instead of merging Config_Database into Kohana::$config (along with Config_File that is placed there first), I put it into it's own var, I don't have issues. This loses the benefits of config merging though, so I consider this an untenable solution.

    At this point, I've been trying to solve this for a few days, and I'm completely stumped. I know there has to be an infinite loop to chew through so much RAM, but I can't figure out where or how. Any input would be appreciated.

  • Unless you already know which SQL statements that are causing the problem, consider logging your SQL statements before they are executed and look for a pattern. If it's always on one SQL query, post that query and maybe we can help.

    If it's on connection/disconnection to the database, you may want to consider the same as above but then manually running those simple connection queries without Kohana to see if it may be the MySQL database causing the problem. Be sure to have a look through all the queries before making any assumptions.

    Just some thoughts, good luck

  • If @joelpittet's advice doesn't help, try and come up with a way to reliably reproduce the issue, and file a ticket with the steps (including any DB data etc etc).

  • Getting closer to finding the problem, I think. I've found what is looping at least, though not yet why.

    I inserted a print statement at the top of the load() method in system/classes/kohana/config.php The output from it tells me something at least:

    Loading group: cookie... 
    Loading group: media... 
    Loading group: session... 
    Loading group: userguide.api_browser... 
    // Config_Database attached at this point, just before Auth::instance() is called
    Loading group: auth... 
    Loading group: database... 
    Loading group: database... 
    Loading group: database... 
    // Snip ~128MB of database group loading
    Loading group: database...
    Out of memory error

    I tried having it log all sql queries before they were executed, but it seems it's not ever getting that far. I know for certain that it's dying before attempting request execution.

    I'm still working to figure out exactly why it's attempting to load that config group repeatedly, but if this brings anything new to mind, I'd appreciate your thoughts.

    I still can't find any pattern to when this begins to happen, so I'm leaving it in a broken state for the moment as I try to understand the cause.

  • Sounds like you are trying to load the database config from the database, which doesn't work of course.

  • After reading a little more, that's exactly what is happening.

    /system/classes/kohana/config.php line #120
            foreach ($sources as $source)
                if ($source instanceof Kohana_Config_Reader)
                    if ($source_config = $source->load($group))
                        $config = Arr::merge($config, $source_config);

    As it iterates through sources (Config_File and Config_Database), it tries to load settings from the database. Since it doesn't have the database config info yet, it tries to load that. Since it's trying to load the database config group, we're back at the start as it tries to load the database config group from the database. Thus, loop.

    How do I tell it to stop trying to load the database config group from the database and only use the file?

  • This sounds like it could be a bug, please can you make an issue on the tracker?

  • I'm also getting this too, in Kohana 3.2. I had to comment out this line so that it'll work:

    Kohana::$config->attach(new Kohana_Config_Database, FALSE)
  • Or you can simply load database config right before adding the Config_Database

    Kohana::$config->attach(new Config_Database, FALSE);

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion