in reply to memory deallocation

usually, the memory is freed by the gc to let be used again by the interpreter, but not freed to the OS. (if a malloc is called, the libc verify if there is enough space on data segment allocated by the process, if not the syscall sbrk is called to increase the data segment. i will be surprised to know that freeing enough memory could defragment it and release it to the OS, and i also suspect sbrk cannot be called to reduce it anyway)

Oha

update: after some tests, i found that allocating memory for an array will free to OS after undef-ing the array, but that will not apply to the data in the array itself:

my @x; system "ps aux | grep $$ | grep [p]erl"; $x[1_000_000] = "foo"; system "ps aux | grep $$ | grep [p]erl"; undef @x; system "ps aux | grep $$ | grep [p]erl"; push @x, "$_" for(1..1_000_000); system "ps aux | grep $$ | grep [p]erl"; undef @x; system "ps aux | grep $$ | grep [p]erl"; ------ oha 8305 0.0 0.2 3296 1232 pts/0 S+ 19:55 0:00 perl t4 +.pl oha 8305 0.0 0.9 7204 5148 pts/0 S+ 19:55 0:00 perl t4 +.pl oha 8305 0.0 0.2 3296 1244 pts/0 S+ 19:55 0:00 perl t4 +.pl oha 8305 76.0 8.6 46896 44800 pts/0 S+ 19:55 0:00 perl t4 +.pl oha 8305 98.0 7.8 42796 40704 pts/0 S+ 19:55 0:00 perl t4 +.pl
the mem freed to OS after the second undef is pretty close the same as the first (difference), so only the memory allocade by the strings aren't freed to OS. i still wonder why...

Replies are listed 'Best First'.
Re^2: memory deallocation
by Fletch (Bishop) on Oct 01, 2007 at 14:35 UTC

    Actually some OSen do allow you to shrink your process size with sbrk (FreeBSD for one). I believe if your perl is using the system malloc you'll get this behavior there (FreeBSD). But in general you should only count on the behavior described above.

      Windows is such a platform too. On Windows ActivePerl 5.8.8, the undef causes the memory usage to go from 171MB to 149MB. I wonder why so little memory is released.
Re^2: memory deallocation
by Nova (Novice) on Oct 01, 2007 at 17:45 UTC
    Hi oha , I am very grateful to you and the other people who have shared their insights with me regarding the question I posed earlier today . I hope that those other than oha will also receive this thank you note , I am just learning how to get around at this time , within this amazing forum . In my initial code , I thought that because I was pushing anonymous arrays into @X , somehow , in the undef of @X the anonymous arrays still existed somewhere and took up MEM . With your code oha , you create and initialize a 1d array , and it seems as though undef behaves differently when the array elements have been initialized to some value / string as opposed to being unitialized. In my previous coding work , I had always assumed , undef -ing an array which say took up 100 MB , would release the 100 MB back to the system . I am still trying to assimilate the information you have all provided. Thank you . Nova .