in reply to Memory Use/Garbage Collection: Java vs Perl

Java doesn't garbage-collect until the variable 'exits scope'. While Perl's garbage-collection has very ill-defined timing, it can collect anything that is syntactically unusable.

This makes the print statement (and its Java analog) the most likely issue.

  1. In Java, you have two string concatenations. All three initial strings and both concatenations will be fully allocated until they 'leave scope'. If you don't carefully place braces so that the temporary variables leave scope before the recursive call, they will be allocated until the program crashes. Did you carefully place those braces in the Java version?
  2. In Perl, both implicit temporaries (if indeed, they are distinct rather than a cleverly reallocated single) are collectable the instant the print statement is over.

So a clumsy Java translation will literally permanently allocate more memory on each recursion. The overhead of Sun's JVM is also much larger than the overhead for Perl. For the more recent ones, Sun recommends assuming 64MB of RAM just to launch their JVM.

Also, Java finalizers execute asynchronously, and are not guaranteed to be executed after the last access of the data object they finalize. A priori, the Java crash could be due to the program attempting to use a finalized (garbage-collected) object. (Perl has similar issues when globally shutting down modules -- but this is after the Perl program is finished.)

  • Comment on Re: Memory Use/Garbage Collection: Java vs Perl