PugSA has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I use a module wich calls a CLI for a specific Java software I use. I work on a win32 machine

When I call the module it looks like this

use appiqcli; my @eventlist = appiqlist(-event -all);

I then print the array to a file. After the script has compleded the CLI clears the virtual memory on my machine. But when I put the script in an endless loop, see code below and I break the loop it does not release the memory (I think it is because I break the script while it might still use the Java and does not have the oppotunity to clean the memory). My question is how can I write a while that loops untill I press a keystroke or send the script an instruction to stop. This will still allow the script to close and release the memory

use appiqcli; while(1) { my @eventlist = appiqlist(-event -all); sleep(10); }

Replies are listed 'Best First'.
Re: break out of enless loop does not release memory
by bart (Canon) on Feb 21, 2007 at 12:23 UTC
    I think you're being bugged by the typical problem that releasing contents of variables doesn't release the memory back to the OS. That only happens when the program quits.

    You don't seem to be in a hurry. Why not just restart the program? This script shows the concept:

    use appiqcli; my @eventlist = appiqlist(-event -all); sleep(10); exec $^X, $0;
    You may have to add any extra parameters.

    Note: $^X is the full path to the current perl interpreter, $0 is the name of the script. And yes, it works on Win32.

    You can watch out for a keypress in that sleeping period, and not do the exec statement if you see one. Or, just have the user press ctrl-C.

      Interisting concept. It ried your Idea but what happend is the virtual memory grew every time it executed the exec

      Interesting was: when I ran out of virtual memory I had to close the cmd.exe and the windows popped up the END NOW window, when I clicked END NOW it released a block of the used virtual memory, any Ideas why this happens

      Thank you for your time

        How are you running this script? I would expect the process to end when the script ends, but some engines keep the process in memory and just restart the script. Your comments about cmd.exe are interesting, and implies some kind of zombie process, but they should not take much memory, even on Windows (a couple of 4kb pages, last time I looked).
Re: break out of enless loop does not release memory
by perrin (Chancellor) on Feb 21, 2007 at 12:47 UTC
    Try undef @eventlist. In most cases, the process will still hold onto the memory, but this may at least let you reuse it for other variables within this program.
Re: break out of enless loop does not release memory
by zentara (Cardinal) on Feb 21, 2007 at 13:59 UTC
    Another trick to try is to re-use the array, by making it a global. That should make the memory rise to the peak of any single run.
    use appiqcli; my @eventlist; while(1) { @eventlist = appiqlist(-event -all); sleep(10); @eventlist = (); }
    Otherwise you will need to write it in C.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum