Subject: | Memory leak in JNI |
I am experiencing a memory leak in when using JNI. It seems to happen
when a string is returned from Java to Perl, and the amount leaked depends
on the size of the returned string (about 128 bytes per function call, plus about 15 bytes per character returned.) It will eventually die with:
Exception in thread "main" java.lang.OutOfMemoryError
Can't call ProcessCommand in class InlineJavaServer at /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Inline/Java/JVM.pm line 405.
Changing the JNI.xs as follows:
[root@komodo jjanes]# diff /root/.cpan/build/Inline-Java-0.47/Java/JNI.xs .cpan/build/Inline-Java-0.47/Java/JNI.xs
291a292
Show quoted text
> (*(env))->DeleteLocalRef(env, resp);
seems to fix the leak. There still may still be a smaller
leak (about 50 bytes per call, independent of the size of the return
string), but I haven't run it until memory was exhausted, so it could
simply be the garbage collector being lazy.
I can follow the leak with the below program
(the `ps` command, used to follow mem size, assumes unix-like OS):
#!/usr/bin/perl -w
use strict;
use Devel::Leak;
$|++;
#use lib "/home/jjanes/perl/lib/perl5/site_perl/";
use Inline Java=> 'DATA', JNI=>1 ;
my $h = new Converter();
my $x=0;
my $leak;
while (1) {
my $y=$h->to_mol($x x50);
# die unless $y eq $x;
print join ("\t" ,Devel::Leak::NoteSV($leak), $x, (`ps -p $$ -o rss `)[1]), "\n" unless ++$x%10000;
};
__END__
__Java__
class Converter {
public Converter() {
};
public String to_mol(String smiles) {
//return smiles;
return smiles.substring(1,20);
};
};
Thanks,
Jeff Janes