Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: forced garbage collection

by quidity (Pilgrim)
on Nov 30, 2000 at 08:13 UTC ( #44080=note: print w/replies, xml ) Need Help??

in reply to forced garbage collection

Once perl has used some system memory for something, you'll be damm lucky if you get it back for other processes to use until perl exits. undef'ing a variable will return the memory used by that variable to perl, so it won't need to eat any more memory the next time it needs some.

This is one of the reasons why eating a whole file into memory is a bad thing, even if you throw it away soon after doing so.

If avoiding memory bloat is important, then you'll need to design an algorithm which doesn't use much in memory storage. Working on small chunks of data and saving the results to a file, then at the end of your program taking the saved results and putting them together into your final answer. Also consider using DB_File, although remember that disk access is much slower then RAM access.

Replies are listed 'Best First'.
(tye)Re: forced garbage collection
by tye (Sage) on Nov 30, 2000 at 10:33 UTC

    One way (under almost every operating system -- AmigaOS being a notable (almost) exception) to return allocated memory so that other processes can make use of it is to exit or exec. Etc.

    So an alternate solution is to have your memory hog restart itself by execing itself. To do this, you'll probably need to persist some state from the old to the new instance of the script. Sometimes this is as simple as passing some command-line parameters to the new instance. Other times you might have to serialize some data structures to a temporary file to be restored by the new instance. Or you can even start the new instance and serialize your state down a pipe to it before the old instance exits.

    Oh, and, just to stress the point, Perl doesn't have a traditional garbage collector. There is nothing to trigger. "Garbage" is "collected" when the number of references to it goes to zero (except for "temporary" garbage which may not be collected until roughly the end of the statement). undef does remove references and so will "collect" "garbage" unless you've made other references to that garbage.

            - tye (but my friends call me "Tye")

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://44080]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2021-10-22 03:05 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (85 votes). Check out past polls.