in reply to Why I got nearly out of memory, and never recover from that?

How does Perl manage memory? Does an object be automatically released, if it is only refered as a local variable within a block?

Yes, it's released, but not given back to the operating system. Which means that the memory can be used for other Perl variables, but not from other processes.

Another question: In loop conditions, after each time of run, does the memory of local variables -- which are inited within the loop BODY(not the heading of loop) -- being deleted and released, and recreated next time?

Yes. (Although there's an optimization which takes care that only the contents of the variable are deleted, not the variable itself).

  • Comment on Re: Why I got nearly out of memory, and never recover from that?

Replies are listed 'Best First'.
Re^2: Why I got nearly out of memory, and never recover from that?
by Marshall (Canon) on Jun 16, 2009 at 07:12 UTC
    I think the Op has a "memory leak" for some reason. There are a bunch of ways that this could happen. But this post is right, Perl will re-use memory if it can. It NEVER gives memory back to the O/S and there are many ways that Perl memory gets "re-cycled" within Perl.

    A "long lived" Perl program will grow to a maximum memory "footprint" and THEN that size remains constant. If you see that memory is always increasing (and not "leveling off"), then one of your objects is not releasing its memory for Perl to recycle. The main point here is that a Perl program will reach a max memory size and that's it!

      To all above:

      The most direct problem it caused is: it seems using hard disk prior than "true" memory, which caused the program runs rather slow. How could I improve it?

        You'll need to modify your program so it doesn't need as much memory at once.

        The most direct problem it caused is: it seems using hard disk prior than "true" memory, which caused the program runs rather slow. How could I improve it?
        It's not surprising your program uses the disk a lot. In your original post, you say your program reads in a large file. Said file lives on disk. To get it from disk, it needs to use the disk. Hence the disk usage.

        Of course, it could also mean you're using so much memory the process is swapping to disk. In that case, you may want to look at your program again and see whether you really need to complete file in memory.