niner / inline-python-pm

Inline::Python - Write Perl subs and classes in Python.
https://metacpan.org/release/Inline-Python
20 stars 13 forks source link

Leaks memory on dictionaries with 'unicode' keys #12

Closed pypt closed 7 years ago

pypt commented 7 years ago

Hello,

Inline::Python seems to leak memory with dictionaries using unicode keys (so with dictionaries using u'foo' keys on Python 2.7 and all dictionaries on Python 3.5). To replicate:

  1. inline_python_memory_leak.pl:
#!/usr/bin/env perl

use strict;
use warnings;

my $count = 0;
while ( 1 ) {
    ++$count;
    my $dict = python_dictionary();
    if ( $count % 10_000 == 1 ) {
        say STDERR "Getting Python dictionary for the $count time...";
    }

}

use Inline Python => <<'PYTHON';

def python_dictionary():
    return {
        'aaaaaaaaaaaa': { 'bbbbbbbbbb': [ 'ccccccccccc', 'ddddddddddd' ] },
    }

PYTHON
  1. Test with Inline::Python built against Python 2.7:
INLINE_PYTHON_EXECUTABLE=python2.7 cpanm --reinstall Inline::Python
perl inline_python_memory_leak.pl
# VIRT and RES usage stays stable
  1. Test with Inline::Python built against Python 3.5:
INLINE_PYTHON_EXECUTABLE=python3.5 cpanm --reinstall Inline::Python
perl inline_python_memory_leak.pl
# VIRT and RES usage grows indefinitely until system starts swapping

Tested on OS X 10.12.1 with Perl v5.24.0, same behavior on Ubuntu 12.04 / 16.04.