On Thursday 13 May 2010 14:56:37 Akhthar Parvez K wrote: > On Thursday 13 May 2010, Akhthar Parvez K wrote: > > On Thursday 13 May 2010, Shlomi Fish wrote: > > > Then it is very likely that the memory allocated to the "mem" pointer > > > will not be returned to the kernel due to the nature of malloc() and > > > how it is an abstraction above sbrk(): > > > > > > http://linux.die.net/man/2/sbrk > > > > > > perl 5 makes use of sbrk eventually as well (either by using the libc > > > malloc() or by implementing its own). Note that for very large > > > allocations, perl may use mmap() instead of sbrk() in which case the > > > memory may get released. I've seen it in a demonstration program that > > > someone pasted on IRC, a few years back. > > > > Yes, I think the memory space allocated by a process using malloc() can't > > be really freed up until the calling process is terminated. And > > processes that uses mmap() can unmap those chunk size of memory when > > it's not needed. I guess Perl by default allocates memory using malloc() > > and changes the memory allocated using sbrk(). So how does Perl know it > > has to mmap() instead if the (huge) size was being used incrementally > > (not at once)? I now believe the only way to free up the space used by a > > small portion of your program is forking that part of the program so > > that it will run as a child process and return all the memory used by it > > back to the system. Please correct me if I am wrong. > > It looks like the memory that is freed (ie, not being used any more) by a > process can be used by other processes, right? I think the *nix OS lets > other processes to use these freed pages by moving them to swap.
Well, if everything works well, the memory pages free() by the process and not accessed again by using a malloc() call to them, will get swapped out and left out of the global RAM or recently-used memory pages. However, the computer still has an amount of global memory available as dictated by the processor , motherboard and the amount that was configured by the operating system. As far as the kernel knows a recently-unused page can still contain memory that was malloc()-ed and still needed by the process - just one that was not accessed in a long time (for some values of "long" that matter to the kernel). As a result, such computer memory that was malloc()ed and then free()ed and the process remained running may eventually cause your system to run out of memory. That's my understanding based on what I've learned and still remember about operating systems and computer architectures, but if you want to get a more accurate opinion, then ask a local kernel expert. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Funny Anti-Terrorism Story - http://shlom.in/enemy God considered inflicting XSLT as the tenth plague of Egypt, but then decided against it because he thought it would be too evil. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/