Re: How to return unused memory to OS?
by eyepopslikeamosquito (Archbishop) on Jan 11, 2008 at 07:37 UTC
|
| [reply] [Watch: Dir/Any] |
Re: How to return unused memory to OS?
by chromatic (Archbishop) on Jan 11, 2008 at 06:02 UTC
|
Is there anyway out make to return that unused memory to OS?
Write your own malloc() and configure Perl to use that. It's not impossible, but there are a lot of niggly little bookkeeping details, especially if you want to avoid fragmentation.
| [reply] [Watch: Dir/Any] [d/l] |
Re: How to return unused memory to OS?
by zentara (Archbishop) on Jan 11, 2008 at 15:40 UTC
|
A cheap way to release a large chunk of memory back to the OS, once a Perl script is done with it, is to fork and exec the memory intensive code, then somehow store the results to be used later. That way, your original Perl script does not retain the memory, at the expense of extra design considerstions to save the returns from the forked code in a db or file. If you want to keep everything in a single script, the only way it will work is if your data is a big string.
#!/usr/bin/perl -w
use strict;
$| = 1;
{
my $string;
for ( 1 .. 100000 ) {
$string .= ( 'x' x 1000 );
}
print "press enter to release";
<>;
undef $string;
print "undefined but still in scope of sub, hit enter\n";
<>;
# if the variable only goes out of scope.
# you *need* to undef it!
}
print "ok,out of scope, press enter to exit";
<>;
This will not work with arrays nor hashes, but if you can somehow stringify your data, the above may work for you.
| [reply] [Watch: Dir/Any] [d/l] |
Re: How to return unused memory to OS?
by CountZero (Bishop) on Jan 11, 2008 at 05:57 UTC
|
Your analysis is basically correct. Once Perl has claimed the memory from the OS it will not be returned to the OS' pool.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
Most interesting and as usual I have to bow before your superior knowledge of these things.I will just add that I never saw the memory used by my Perl scripts get released back to the OS, but that may say more about my scripts than anything else. I have few long running scripts (except some mod_perl applications) so that may be the reason and I try to be parsimonious with memory so there is probably never enough in one block that can get released back to the OS.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [Watch: Dir/Any] |
|
Perl uses a different allocation system on Windows than on other systems by default. You will experience differences in this are as a result.
| [reply] [Watch: Dir/Any] |
|
Hmmm...no matter what I do, windows does not reallocate that memory. I'm talking hundreds of megabytes of memory that are not reallocated. There seems to be nothing automatic about it....at least not in my case.
Sounds like I need to use siding windows (as suggested by Rolf) or just deal with half of my memory being dedicated to a very simple task for days on end.
| [reply] [Watch: Dir/Any] |
Re: How to return unused memory to OS?
by BrowserUk (Patriarch) on Jan 11, 2008 at 06:25 UTC
|
Which OS?
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.
| [reply] [Watch: Dir/Any] |
|
I am running my application on Linux and Solaris
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: How to return unused memory to OS?
by weismat (Friar) on Oct 27, 2009 at 19:46 UTC
|
Are you anyhwere setting any variables to undef or taking it out of scope?
As you pointed out this is reference counting, thus there needs to be no reference so that it can be garbaged collected. Unfortunately Perl is not as smart as for instance the CLR for .NET to find cyclic references.
One solution could be to refactor your code to work with more functions with local variables.
| [reply] [Watch: Dir/Any] |