On 04/06/2013 10:30 AM, John Redford wrote:
David Larochelle writes:

Are languages that have mark and sweep garbage collection better about
returning memory to the system than languages like Perl that use reference
count garbage collection.



Programs that want to do this kind of thing -- alone or cooperatively --
should use mmap (UNIX) or CreateFileMapping (Windows). This provides the
application with a virtual address mapping that is backed (generally) by a
file on disk.  When the application wants to release that memory, it merely
unmaps the file. At the point where actual content is read or written to the
file, physical RAM pages are allocated & used, but they are released with
the file itself. This is generally the technique programs should use when
they want control over this kind of thing -- mmap also makes it easy for one
program to map-in what another program has mapped-out, thus instantly
sharing a complex data structure (using offsets rather than absolute
addressing for internal pointers).  If two programs both mmap a file at the
same time, they can share the physical memory and thus communicate with each
other that way (generally also using semaphores for locking).

mmap won't help with returning ram to the system. there is still a malloc/free involved and that will come from the same place as the rest of ram. the only way to return virtual ram is by lowering the top virtual address with brk()/sbrk() calls. and that needs contiguous ram at the top of the address space. and that means freed ram must be collected into contiguous blocks. that is nothing to do with mark/sweep vs ref counting or any other type of gc. mark sweep will use a large contiguous area to copy active data but it needs the other buffer to do it again. it won't ever want to free that ram. the only way this ever happens is the custom code that manages the ram blocks by calling brk() directly. it is too tricky to do in most cases. and with virtual ram it doesn't really matter as i said, it will be swapped out until the process dies if you don't use it again.

uri


There are a number of options which exist other than simply mapping a
regular file -- it is also possible to map directly against swap, and to
control how the mapped pages are addressed and cached and so forth. This is
essentially a user-space mechanism to tell the kernel exactly how certain
segments of memory need to be handled.



_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm




_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to