whay can't processes shrink as well as grow?

Steve Baur slb at neptune.dsc.com
Thu Oct 4 21:26:36 AEST 1990


ggm at brolga.cc.uq.oz.au (George Michaelson) writes:

>My understanding is that existing brk/sbrk/malloc in generic *nix
>doesn't allow the process to shrink again once mem has been allocated.

>Could somebody explain to a non-wizard what stops some method being
>used to detect free mem pages or compress the heap such that memory
>CAN be freed?


The problem has to do with the fact that generic *nix is basically a
swapping system, with virtual memory paging (if it exists at all) added
as an after thought.  This is true through System V/R3.

The UNIX system process data space looks like this:


+-------------------------------+
|       Process Stack           |
|             |                 |
|             V                 |
|                               |
+-------------------------------+
|                               |
|       Free Memory             |
|                               |
+-------------------------------+   <----   "Break"
|                               |
|             ^                 |
|             |                 |
|            Heap               |
|                               |
+-------------------------------+
|                               |
|             BSS               |
|                               |
+-------------------------------+
|                               |
|            Data               |
|                               |
+-------------------------------+


The line above the heap (called the "break") is adjusted via the brk(2) and
sbrk(2) system calls and is adjusted upwards when more memory is needed.
It could also be adjusted downwards when allocated memory just below the
break is released, but that does not seem to be performed through System
V/R3.

A more basic problem exists with this scheme though.  Imagine allocating
a 100k chunk of memory, then a 10 byte chunk of data, then deallocating the
100k chunk.  Now the allocated 10 byte chunk of data sits just below the
break, and hence it cannot be adjusted downwards to reflect the release
of the 100k chunk.  No holes are allowed to exist between the top of BSS
and the break.

Anyway, to properly deal with a paging system, something like a getvmpage/
releasevmpage (that treats virtual memory as pages rather than as an
area below an address) is needed.

--
"The speed of light ...  It's not just a good idea, it's the law."
Steve Baur (slb%neptune%dschub at hub.ucsb.edu)



More information about the Comp.unix.internals mailing list