Re: Unmapping memory

Martin von Loewis (martin@mira.isdn.cs.tu-berlin.de)
Tue, 6 May 1997 10:59:04 +0200


> Would it be possible to add a syscall to "zero" a range of memory? I mean
> not only clear it to zero, as with memset, but if any complete pages are
> within the range, unmap them, and replace them with zero mappings (as
> mmap()'ing /dev/zero gives you).

You could just mmap /dev/zero instead. No need to unmap what was previously
there, it will get automatically discarded. The malloc implementation would
need to keep a file descriptor for /dev/zero to avoid the overhead of
opening the file over and over again. Other than that, there would be no
overhead compared to a special-case system call.

> With this ability, a free() implementation could invoke this call any time
> a largish chunk of memory is truly discarded, and thereby genuinely give
> memory back to the OS, _without_ the significant overhead and limitations
> of using mmap() and munmap().

I think one should perform measurements first to find out:
- how many, and which applications suffer from this phenomen. E.g. after
running Netscape for one day, how many pages could be returned to the
system. LD_PRELOAD is your friend, here.
- for those applications, how much overhead is introduced by inserting the
additional mappings. For example, drivers/char/mem.c:read_zero only changes
the mapping if the user requested at least four pages.
Once those data are available, we could decide whether this algorithm
becomes part of the standard C library, or a special malloc library that
is only linked into selected applications.

Regards,
Martin