in reply to Bare-bones embedding and memory leakage

You're missing the fact that eval_pv returns data which you're not freeing.

See my reply at eval_pv leaking.

  • Comment on Re: Bare-bones embedding and memory leakage

Replies are listed 'Best First'.
Re^2: Bare-bones embedding and memory leakage
by polettix (Vicar) on Mar 16, 2005 at 15:59 UTC
    Hi Joost, thank you for your kind response.

    Actually, I don't see the point of defining the variable in the for cycle. You're assigning a value to a C variable, which actually has no reference count or implicit disposal; moreover, the Perl interpreter has no clue about what you're going to do with the return value (unless you tell it).

    What I'm saying is that your code is equivalent to the following cycle:

    for (;;) { ENTER; SAVETMPS; eval_pv("1;", TRUE); FREETMPS; LEAVE; }
    which has no explicit handling of the return value of eval_pv.

    But I still thank you because your suggestion actually made me think: why do I need PUSHMARK(SP) and SPAGAIN? I removed them and... I had less leakage. (BTW, I'm going to modify my original post to include this enhancement).

    Yes, less but not zero leakage. When my program starts, now, it has the following memory footprint (i.e. what you get before the first "read"):

    Name: embed01 Pid: 13386 FDSize: 256 VmSize: 2696 kB VmLck: 0 kB VmRSS: 940 kB VmData: 256 kB VmStk: 8 kB VmExe: 836 kB VmLib: 1516 kB
    When I let the program go, I notice an increment in VmSize of around 4 kB every million iteration i let go; so, after 4 million iterations, I get:
    Name: embed01 Pid: 13386 FDSize: 256 VmSize: 2712 kB VmLck: 0 kB VmRSS: 1056 kB VmData: 272 kB VmStk: 8 kB VmExe: 836 kB VmLib: 1516 kB
    that is a quite subtle leak IMHO but still a leak. Note that the memory increase is quite slow and time-linear, i.e. if I stop at 2 million iterations I get 2704 kB or something similar.

    This is quite an improvement with respect the previous version, but my question remains more or less the same: where is it leaking? Moreover: if I'll ever actually need PUSHMARK(SP) and SPAGAIN... how will I avoid leakage?

    TIA, --Flavio

      why do I need PUSHMARK(SP)
      You don't need PUSHMARK, and in fact using it there is wrong. You only need it when the the thing you're calling expects a list of args on the stack (eg when calling call_pv()). Here, you're using eval_pv(), which doesn't expect a list of args, so using it is wrong.

      VmSize of around 4 kB every million iteration
      IIRC, there was a bug that caused every 65536th allocated op not to be freed; this is fixed in recent Perls. You don't say which version you're using, but try it with something like 5.8.6 and see if the residual leak goes away.

      Dave.

        Thank you for the advice. I'll make penance in my cell to espiate my lack of carefulness in providing useful information to the Wise Monks.

        As a novice, I ask you comprehension for being still stuck on ancient perl, v5.8.0 built for i386-linux; I'll try to remedy ASAP and rescue myself.

        --Flavio

        Woah, I'm a happy novice now!

        I just installed the last stable version of Perl (according to your suggestion) and eventually managed to get rid of the memory loss problem!

        Incidentally, I also realised (thanks to the compiler) that the dSP line was superfluous as well in my do_something function.

        -- Don't fool yourself.