http://www.perlmonks.org?node_id=1073925


in reply to Perl and memory usage. Can it be released?

On Linux, the glibc malloc behavior is influenced by environment variables. Large allocations are performed via mmap, smaller chunks usually live on data segment arena, which grows or shrinks via brk. Default mmap threshold might be 128k. For example:

$ strace -e brk,mmap perl -e 'pack q(x200000)'
...
brk(0x79f000)                           = 0x79f000
mmap(NULL, 200704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = ...

$ export MALLOC_MMAP_THRESHOLD_=300000
$ strace -e brk,mmap perl -e 'pack q(x200000)'
...
brk(0x79f000)                           = 0x79f000
brk(0x7db000)                           = 0x7db000
brk(0x7aa000)                           = 0x7aa000
First time, the memory was obtained via mmap; second time, by growing the arena. Arena may also shrink (here it was possible), but even if it doesn't, the unused pages are typically not much of a concern. (mmap-ed storage is unmapped when freed.)

If the process is long-lived, does great many allocations at various stages, then memory fragmentation may become a problem. (Web browsers come to mind.) When processing file after a file as you describe, this is unlikely to matter either. Memory gets allocated and released in full every time. Just be sure there are no leaks.