Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

fork kills Inline::Java

by Sixtease (Friar)
on Mar 20, 2008 at 17:11 UTC ( #675258=perlquestion: print w/replies, xml ) Need Help??
Sixtease has asked for the wisdom of the Perl Monks concerning the following question:

Hello friends,

I'm using a Java application from within a Perl script via Inline::Java. God bless that module. I have another Perl module that leaks huge amounts of memory and I need that one to run before the Java thing but after Java has been loaded. I need to reclaim the leaked memory before letting the Java app do its business, so I fork and do the leaking in a child process, which, after exiting, frees the memory.

What causes me grief is that after the child process ends, my Java no longer works. I attribute it to destructors that cripple my JVM when the child process deceases. Undefing DESTROY methods doesn't help. I guess it would be best to somehow invoke fresh new perl interpreter to do the dirty leaking job instead of forking but I don't know how to do it (of course it would be best to not leak but the module is not mine and it's huge, obfuscated and sui generis). Or is there a better way?

Here is a fork-enhanced snippet from perldoc Inline::Java that demonstrates my problem:

use Inline Java => <<'END_OF_JAVA_CODE' ; class Pod_alu { public Pod_alu(){ } public int add(int i, int j){ return i + j ; } } END_OF_JAVA_CODE my $alu = new Pod_alu() ; print($alu->add(9, 16) . "\n") ; if (fork == 0) { exit(0) } else { wait } print($alu->add(9, 16) . "\n") ;

Oh and happy Easter :-)

use strict; use warnings; print "Just Another Perl Hacker\n";

Replies are listed 'Best First'.
Re: fork kills Inline::Java
by samtregar (Abbot) on Mar 20, 2008 at 18:40 UTC
    If I remember correctly Inline::Java communicates with a separately running JVM via a socket connection. If that's true then it may be that when your child process dies it's closing the socket connection (which it shares with the parent) and that's borking your connection to the JVM.

    One way to solve the problem would be to replace your fork() with a system() call that starts a new Perl process to perform your leaky code:

       system("$^X -MSerious::Leaker -e 'Serious::Leaker->leak_on()'");

    If that's impractical, you might look at the JNI and shared JVM options for Inline::Java ( It seems like either one might solve this problem. The shared JVM option seems particularly attractive since it also may improve performance.


Re: fork kills Inline::Java
by jplindstrom (Monsignor) on Mar 20, 2008 at 22:39 UTC
    If running the DESTROY/END routines is in fact the problem, it may work to end the process by calling POSIX:_exit() instead.


Re: fork kills Inline::Java
by Sixtease (Friar) on Mar 21, 2008 at 07:12 UTC
    it may work to end the process by calling POSIX:_exit()

    It helped! It helped! :-)

    Thank you both for your good advices. *bows*

    use strict; use warnings; print "Just Another Perl Hacker\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://675258]
Approved by pc88mxer
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2018-05-28 09:08 GMT
Find Nodes?
    Voting Booth?