Grinnz / Mojo-SQLite

Mojo::SQLite - A tiny Mojolicious wrapper for SQLite
https://metacpan.org/pod/Mojo::SQLite
Other
27 stars 12 forks source link

Memory leak #7

Closed Htbaa closed 7 years ago

Htbaa commented 7 years ago

Hello,

It seems there is a memory leak in Mojo::SQLite which showed itself in long running Minion workers. I mentioned it on the Minion::Backend::SQLite GitHub page as a possible leak in that module (see https://github.com/Grinnz/Minion-Backend-SQLite/issues/8), but it seems I've traced it back to Mojo::SQLite.

Anyway, I'll copy-paste part of it here:

Running this script:

use strict;
use Mojo::SQLite;
use Memory::Usage;
use v5.20;

my $mu = Memory::Usage->new;
my $sql = Mojo::SQLite->new('sqlite:/tmp/test.db');
$mu->record('before');
for(1..10000) {
    $sql->db->query('select sqlite_version() as version');
}
$mu->record('after');
$mu->dump();

Which results in:

time    vsz (  diff)    rss (  diff) shared (  diff)   code (  diff)   data (  diff)
    0  74392 ( 74392)  21920 ( 21920)   5064 (  5064)   1852 (  1852)  17240 ( 17240) before
    1  80004 (  5612)  28312 (  6392)   5696 (   632)   1852 (     0)  22820 (  5580) after

As you can see the data diff has increased.

Simply calling $sql->db is enough for it to leak actually, no need to even execute a query.

Grinnz commented 7 years ago

I believe commit a2d06a6 resolves this. For some reason re-setting the update hook whenever the dbh was dequeued again was causing a leak. (and also unnecessary work anyway)

Grinnz commented 7 years ago

Fixed in 1.002