mirasvit / module-profiler

Magento 2 Profiler
https://mirasvit.com/
Open Software License 3.0
133 stars 36 forks source link

Memory leak on CLI commands with module installed #29

Open Tomasz-Silpion opened 6 years ago

Tomasz-Silpion commented 6 years ago

There seems to be some kind of hole that causes lot of memory leaking on php bin/magento CLI usage on custom scripts or even Mirasvit_ElasticSearch usage having the Mirasvit_Profiler installed on the store.

Wasted some hours of the team for debugging custom products importer, downgrading the store and plugins. We've finally figured out, that without any warning the reason of 20MB being added to memory through each loop element comes from the module-profiler :-1: :(

george-vlahakis commented 4 years ago

It is actually not a memory "leak" precisely. The profiler naturally stores information during operations. However I do agree CLI commands should not trigger the profiler...

I too wasted 3 days on this going down to PHP source build and MySql drivers check... pfff 🤬

Can be changed here I suppose to force CLI to $canEnable = false:

$_SERVER['MAGE_PROFILER_STAT'] = new \Magento\Framework\Profiler\Driver\Standard\Stat();

$canEnable = defined('BP');

if (PHP_SAPI == 'cli') {
    $canEnable = false;
    // global $argv;
    // if (isset($argv[1]) && substr($argv[1], 0, strlen('setup')) == 'setup') {
    //     $canEnable = false;
    // }
}

if ($canEnable) {
    \Magento\Framework\Profiler::applyConfig([
        'drivers' => [
            [
                'output' => 'Mirasvit\Profiler\Model\Driver\Output\Html',
                'stat'   => $_SERVER['MAGE_PROFILER_STAT'],
            ],
        ],
    ], 'BP', false);
}

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Mirasvit_Profiler',
    __DIR__ . '/src/Profiler'
);