Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Memory reusability

by abubacker (Pilgrim)
on Aug 12, 2009 at 05:53 UTC ( #787839=perlquestion: print w/replies, xml ) Need Help??
abubacker has asked for the wisdom of the Perl Monks concerning the following question:

Dear community,
My trainers says that Perl doesn't supports memory reusability
can't I free a memory and assign to some other ?
Is it true ?
It yes why is it so ?

Thanks in advance !

Replies are listed 'Best First'.
Re: Memory reusability
by busunsl (Vicar) on Aug 12, 2009 at 05:58 UTC
    Perl does support reuse of memory, but:

    The perl binary aquires memory for the perl program but this memory is never released to the operating system as long as the perl program runs.

    So inside a running program the memory is reused but never freed.

      Dear busunl ,
      Do you meant to say that each perl program will consume their required memory when it starts ?
      One more question is whether it acquires more memory during run time

        It does not acquire all memory when it starts.

        It will get more memory during runtime when needed.

        There are two major points in perl memory management:

        1. You don't have to bother, perl will do that for you
        2. Memory will not be freed during runtime

        That's all.

Re: Memory reusability
by BioLion (Curate) on Aug 12, 2009 at 08:03 UTC
Re: Memory reusability
by BrowserUk (Pope) on Aug 12, 2009 at 12:18 UTC

    It depends upon the version and platform of Perl.

    Under Windows, perl can and does release some memory back to the OS when it is done with it, as demonstrated below:

    c:\test>p1 Name "Data::Dump::MAX_WIDTH" used only once: possible typo at C:\Perl6 +4\bin\p1.p [0] Perl> system qq[ tasklist /fi "pid eq $$"];; Image Name PID Session Name Session# Mem + Usage ========================= ======== ================ =========== ====== +====== perl.exe 5600 Console 1 9 +,972 K [0] Perl> $big = 'x' x 10e6;; [0] Perl> system qq[ tasklist /fi "pid eq $$"];; Image Name PID Session Name Session# Mem + Usage ========================= ======== ================ =========== ====== +====== perl.exe 5600 Console 1 19 +,764 K [0] Perl> undef $big;; [0] Perl> system qq[ tasklist /fi "pid eq $$"];; Image Name PID Session Name Session# Mem + Usage ========================= ======== ================ =========== ====== +====== perl.exe 5600 Console 1 9 +,976 K

    And I've heard tell, but cannot verify that under some circumstances, Perl will do the same on some other platforms. YMMV.

    But in the end, it is irrelevant, and your trainers should be taken to task for even mentioning it at your stage of learning.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Memory reusability
by cdarke (Prior) on Aug 12, 2009 at 09:33 UTC
    The question is worrying. It implies that you cannot ask the trainer about this, and you should. Maybe you could ask your trainer how Perl manages its heap, and how heap memory is managed by the operating system for reuse.

    Does the trainer mean that other comparible languages behave in a differnt way? That is the implication of the question.

    Others have answered your question in detail concerning Perl, but be reassured that Perl is no worse in its memory handling than other dynamic languages, and some would argue it is better.
      Does the trainer mean that other comparible languages behave in a differnt way? That is the implication of the question.

      I would, personally, not imply something which hasn't been asked, but indeed maybe an even more interesting question would be: Is there ANY modern (i.e. post-C++) language at all which does NOT handle memory allocation automatically? I don't talk about ongoing developments in "old" languages (such as FORTRAN, C or COBOL), or newer ones which for compatibility reason have to carry some burden from an older language (such as C++).

      -- 
      Ronald Fischer <ynnor@mm.st>
Re: Memory reusability
by rovf (Priest) on Aug 12, 2009 at 09:12 UTC

    From the viewpoint of language definition of Perl, there is nothing which dictates that memory must be reused, though an implementation of Perl, which does not free memory, would hardly be useful except for the most simple tasks. So I would say: The question whether "Perl" (as a language) reuses memory, is meaningless, but all existing Perl implementations certainly re-use memory. Of course this does not mean that part of the memory not used by Perl, automatically can be used by other application. This question belongs to the architecture of the undrelying OS, and is not related to Perl.

    can't I free a memory and assign to some other ?
    Yes and no. You can not force Perl to re-use memory in a way you dictate (and I don't see a reason why you would want to anyway), but memory is re-used automatically, on the fly. For instance, in the following program
    my $a='abcd'; ... $a='efgh';
    the space occupied by the string 'abcd' is not lost, but will eventually be used later on for something different.

    -- 
    Ronald Fischer <ynnor@mm.st>
      Of course this does not mean that part of the memory not used by Perl, automatically can be used by other application. This question belongs to the architecture of the undrelying OS, and is not related to Perl.
      That's not entirely true. Certainly, whether or not the OS is able to accept memory back from the OS isn't related to perl. But if perl runs on an OS that does allow for processes to return memory back to the OS during the lifetime of the process, then it becomes related to perl. Because then perl would actually have to make the appropriate system calls to give memory back.
        then it becomes related to perl.

        Thank you for clarification. To be more precise: It becomes related to the Perl implementation (not to Perl as a language), i.e. ActiveState Perl could do it one way and Strawberry Perl could do it another way.

        -- 
        Ronald Fischer <ynnor@mm.st>
        Because then perl would actually have to make the appropriate system calls to give memory back.

        Perl should just use standard free(3) function and this function will make appropriate system call to return memory to OS.

Re: Memory reusability
by apl (Monsignor) on Aug 12, 2009 at 11:29 UTC
    If (as you say in Re^2: Memory management) you're writing an OS in Perl, but (as this post implies) you're just starting to learn Perl, I'd suggest you find a simpler project to start with ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://787839]
Approved by ww
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2018-06-25 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?