* Re: More questions on swap space reservation
1999-05-26 4:00 More questions on swap space reservation dtmoore
@ 1999-05-26 4:00 ` Serguei Patchkovskii
1999-05-27 4:00 ` dtmoore
0 siblings, 1 reply; 4+ messages in thread
From: Serguei Patchkovskii @ 1999-05-26 4:00 UTC (permalink / raw)
dtmoore (sponk@my-deja.com) wrote:
: Now, I have a FORTRAN77 program which contains lots of fairly
: large statically allocated arrays. When I run it under the agressive
: swap mode it eats up about a third of my swap space (101 MB), but its
: resident memory size is only ~6MB. My question is: Why are the
: resident size and swap size so different? As I understood it, the
: aggressive mode reserved enough space so that the entire memory image of
: a process could be swapped out. Well, in this case, the memory image
: appears to be 6.4 MB, yet it reserves about 16 times that in swap space.
Ok, I guess this is the time for a little lecture on basics of virtual
memory in Unix. Loosely speaking, each unix program consists of three
sections: code, initialized data, and uninitialized data (often called
bss). Code and data sections (plus some overhead) determine the size of
your program's disk image. For obvious reasons, bss does not take any
space in the disk image.
When program is started, neither one of the three sections is loaded
to memory. Instead, the operating system establishes a correspondence
(mapping) between the program image on disk and the virtual addess
space of a running program:
Program code cannot be changed by the running program, and is therefore
mapped read-only, with only the parts (pages) which are being currently
used copied to the main memory. If the O/S runs tight on memory, it can
simply discard any in-memory copies of the code pages: it knows which
executable images they came from, and can re-load them from disk if
they are needed again. If you run several copies of a program, all these
copies share code pages.
Initialized data is mapped with the copy-on-write attribute: until you
attempt to change it, it gets treated in exactly the same way as the
code pages would. However, if you change any variables, the system
will create a copy of the affected page private to the instance of the
running program which accessed it. If the system runs out of memory,
it will have to find some place to store the modified page until its
needed again - or until the program exits.
Finally, the part of the virtual address space corresponding to the
uninitialized data is mapped with the zero-on-access atribute. As long
and the program does not attempt to access it in any way, it simply
does not exist. On the first access (either read or write), the affected
page is allocated from the memory pool and filled with zeros. From
that point, it is treated just like a modified data page, and will have
to be stored somewhere if the memory it occupies is needed for other
purposes.
(Any memory you allocate dynamically can be treated as a part of bss
at this point, and has the same effect on the swap allocation)
Now, we can come to the difference between the immediate (or "aggressive")
and deferred (or "lazy") swap space allocation mode. In the immediate
mode, when a program starts the operating system allocates enough swap
space to store all pages which can *conceivably* have to be stored - that
is for all data pages as well as for all bss pages. This is almost always
an overkill, but *guarantees* that the program will not suddenly run out
of memory it think it owns.
The downside of the immediate mode is that in the cases like yours, there
a program declares huge chunks of bss which are not actually used in a
typical run, the system still must reserve swap space for *all* of them,
requiring inordinate amount of swap space - which is not in fact used.
Fortran-77 programs tend to be the culprit in this situation.
In the deferred mode, the system does not try to allocate the swap space
until it actually needs to store a memory page somewhere. Since there
usually are some, and sometimes quite a few, virtual memory pages which
*could* need backing store (swap space), but do not need it right now,
this usually results in more efficient memory/swap space utilization.
The downside is that a system running in deferred swapping mode may have
no recourse but to kill running applications more or less randomly: if
a program attempts to modify a data page which it had not changed before,
or accesses a new bss page, the system will have to find a new (private)
real memory page for that program. If no free pages are available, the
system will have to free up some of the occupied ones, either by discarding
pages it knows how to reload from disk or swap space ("clean" pages) or
by swapping out modified ("dirty") pages. If the system has no clean pages
it can discard, and no swap space to store the dirty pages, the only action
it can take is to kill the application which needs a new memory page in
the first place. Since this can be just about any program running in the
system, including the programs providing critical system services, this
*may* (but not necessarily *will*) result in the system becoming unusable.
In the case of Tru64/Digital Unix/OSF/1, the operating system attempts to
minize the potential negative impact of the deferred mode by reserving a
certaing fraction of the memory pages for the exclusive use of the privileged
programs. If the out-of-swap condition exists only for a short time, this
helps critical system programs to survive it, riding them over until one of
the user processes is killed, freeing up some memory. It however does not
*guarantee* that the system will remain usable - just improves the odds
of that happening.
I tend to absolutely require immediate swap allocation mode on any systems
which can be used by more than one person or for more than one task. For a
"personal" system, it is probably Ok to enable the deferred mode - provided
that the user of the system knows exactly what he or she is doing...
Regards,
/Serge.P
--
home page: http://www.cobalt.chem.ucalgary.ca/ps/
^ permalink raw reply [flat|nested] 4+ messages in thread