Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonologBundle memory leak (?)

I have a long running process in Symfony2 (rabbit consumer) and I am using the MonologBundle for logging. The lines are logged immediately but I have noticed that the memory consumption of the process is increasing with every iteration, reaching over 1GB after a fiew minutes.

The script runs with: --env=prod

So i made a smaller test:

        // taken from my symfony test command
        $logger = $this->getContainer()->get('logger');

        while (true){
            $logger->debug("line one");
            $logger->debug("line two");
            $logger->debug("line three");
            var_dump($logger);
        }

This is the var_dump content after ~10k iterations:

class Symfony\Bridge\Monolog\Logger#3 (3) {
  protected $name =>
  string(3) "app"
  protected $handlers =>
  array(1) {
    [0] =>
    class Monolog\Handler\FingersCrossedHandler#132 (11) {
      protected $handler =>
      class Monolog\Handler\StreamHandler#133 (9) {
        ...
      }
      protected $activationStrategy =>
      class Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy#134 (1) {
        ...
      }
      protected $buffering =>
      bool(true)
      protected $bufferSize =>
      int(0)
      protected $buffer =>
      array(100) {
        ...
      }
      protected $stopBuffering =>
      bool(true)
      protected $passthruLevel =>
      NULL
      protected $level =>
      int(100)
      protected $bubble =>
      bool(true)
      protected $formatter =>
      NULL
      protected $processors =>
      array(0) {
        ...
      }
    }
  }
  protected $processors =>
  array(0) {
  }
}

Monolog bundle settings:

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: error
            handler:      nested
            buffer_size:  100
        nested:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
            buffer_size:  100


framework:
    profiler:
        only_exceptions: false
        enabled: false
        collect: false 

The log entries in the buffer do not exceed the buffer_limit but the memory usage of the script still increases.

Any ideas? Thanks

PS: I repeated the test with plain monolog and there was no memory issue.

like image 746
Tibor Avatar asked Apr 02 '15 14:04

Tibor


2 Answers

That's fingers_crossed expected behavior. It stores log entries in memory until event of action_level occurs. Change it to stream or some other non-buffering handler.

like image 115
Im0rtality Avatar answered Nov 17 '22 16:11

Im0rtality


You can also limit the amount of logs stored with "buffer_size". See:

http://symfony.com/doc/current/reference/configuration/monolog.html

like image 5
bmlkc Avatar answered Nov 17 '22 18:11

bmlkc