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.

Not in practice.

While that potential is there, in it is not utilized. Most programs that
allocate & then free a lot of memory will either terminate in the near
future or will reallocate that memory again in the near future.  One can
imagine two programs on a system which took turns allocating & then freeing
a lot of memory, such that it would make sense for each of them to return
the memory to the host for it to allocate to the other -- but since such
programs are rare, the chances of having two of them that happen to
synchronize thusly is even lower -- it's just not worth the effort to make
it happen -- particularly because there's another approach which makes more
sense and permits application to exercise fine control over memory use.

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).

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

Reply via email to