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.)


In reply to Re: Memory Use/Garbage Collection: Java vs Perl by zaimoni
in thread Memory Use/Garbage Collection: Java vs Perl by c0d3cr33p

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.