Perl memory allocation

I

Ian Cass

Guys, why doesn't this program release its memory?

use strict;
my $t = "test";
for (my $i = 0; $i < 25; $i++) {
$t .= $t;
}
$t = '';
print "Allocated and freed memory\n";
<>;

When I do a 'ps' to see the memory in use, I see the following

ian.cass 10724 6.4 12.8 136496 132592 pts/0 S 13:44 0:00 perl t.pl

Why isn't $t = '' freeing the memory that was allocated in the previous for
loop?

Tested on Debian Linux 2.6.2 kernel with Perl 5.8.3 and on Redhat Linux
2.4.20 kernel with Perl 5.8.0
 
I

Ian Cass

Eric said:
perldoc -q free

(the second entry displayed is the one you want)

My Perl is compiled with the OS's malloc rather than Perls.

My variable is a global, so it will never go out of scope, but I've tried
"$var = undef;" without any success.

I'll put some code in to re-exec it every 24hrs if I can't find another way
around this issue.

Thanks.
 
E

Eric Bohlman

My Perl is compiled with the OS's malloc rather than Perls.

How is that relevant?
My variable is a global, so it will never go out of scope, but I've
tried "$var = undef;" without any success.

The entry I was talking about said that the space for out-of-scope and
undefined variables becomes reusable by perl for use in other parts of the
program, not by the OS.
 
B

Ben Morrow

Eric Bohlman said:
How is that relevant?

perlfaq3.pod:
| Some operating systems [...] can reclaim memory that is no longer
| used, but on such systems, perl must be configured and compiled to use
| the OS's malloc, not perl's.

was no doubt confusing the OP... the important part is 'Some OSen'.
Clearly the OP's isn't one of them.

(As an aside, would it be feasible to change perl's malloc to use mmap
where possible, and release mem back to the OS? This seems to be quite
often wanted...)

Ben
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Ben Morrow
How is that relevant?

perlfaq3.pod:
| Some operating systems [...] can reclaim memory that is no longer
| used, but on such systems, perl must be configured and compiled to use
| the OS's malloc, not perl's.

was no doubt confusing the OP... the important part is 'Some OSen'.
Clearly the OP's isn't one of them.

Well, given that the FAQ entry is wrong, it is not the only reason for
confusion... The correct answer should be something like "some OSes
in some situations"...
(As an aside, would it be feasible to change perl's malloc to use mmap
where possible, and release mem back to the OS? This seems to be quite
often wanted...)

Do not think that this is often *wanted*; but anyway, a lot of people
may *think* they want this. ;-)

BTW, such a change is in my TODO list for many years; but given that I
can't work on Perl without grants any more, it does not have a large
chance to float to the front of my list...

Hope this helps,
Ilya
 
M

Maximus

My variable is a global, so it will never go out of scope, but I've tried
"$var = undef;" without any success.



$var = undef; ??? What is this?

undef $var; That is!!! :)))
 
C

ctcgag

Ben Morrow said:
Eric Bohlman said:
How is that relevant?

perlfaq3.pod:
| Some operating systems [...] can reclaim memory that is no longer
| used, but on such systems, perl must be configured and compiled to use
| the OS's malloc, not perl's.

was no doubt confusing the OP... the important part is 'Some OSen'.
Clearly the OP's isn't one of them.

(As an aside, would it be feasible to change perl's malloc to use mmap
where possible, and release mem back to the OS? This seems to be quite
often wanted...)

How would it ensure that the entire segment is simultaneously unused
in order to return the underlying memory? Unless you have one mmap per
variable, it doesn't seem very easy. I guess you could add syntax to give
hints:

my_big_assed $x = "The quick brown fox jumps over the lazy dog"x10_000;

Xho
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Bernard El-Hagin
Same thing.

Absolutely not (unless you mean v5.002 or so, do not remember exactly
when I fixed this):
perl -MDevel::peek -wle "$a='asdf';$a=undef; Dump $a"
SV = PV(0x40c64) at 0x5885c
REFCNT = 1
FLAGS = ()
PV = 0x47c48 "asdf"\0
CUR = 4
LEN = 5
perl -MDevel::peek -wle "$a='asdf';undef $a; Dump $a"
SV = PV(0x40c64) at 0x5885c
REFCNT = 1
FLAGS = ()
PV = 0

Hope this helps,
Ilya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top