[ale] Swap Memory usage

synco gibraldter synco at xodarap.net
Fri Sep 19 14:15:12 EDT 2003


On 19 Sep 2003 at 13:34, John Wells wrote:

> I thought the point of swap was to move lower priority data into the
> swap to free up RAM, but then move it back into RAM if its need
> becomes a priority again.  In other words, if an app that uses the
> data is currently in memory and running, any swapped data it uses
> would be loaded back into main RAM provided space was available.  Is
> this not the case?

i think that's right... i'm not a kernel memory expert by any means... but i did find this 
very nice analogy and simplified explanation at:
http://www.linux-tutorial.info/cgi-bin/display.pl?89&0&307&0&3


If you are reading a book, you do not need to have all the pages spread out on 
a table for you to work effectively just the page you are currently using. I 
remember many times in college when I had the entire table top covered with 
open books, including my notebook. As I was studying, I would read a little 
from one book, take notes on what I read, and, if I needed more details on that 
subject, I would either go to a different page or a completely different book. 
Virtual memory in Linux is very much like that. Just as I only need to have 
open the pages I was working with currently, a process needs to have only 
those pages in memory with which it is working. Like me, if the process 
needs a page that is not currently available (not in physical memory), it needs 
to go get it (usually from the hard disk). 
If another student came along and wanted to use that table,there might be 
enough space for him or her to spread out his or her books as well. If not, I 
would have to close some of my books (maybe putting bookmarks at the 
pages I was using). If another student came along or the table was fairly 
small, I might have to put some of the books away. Linux does that as well. 
Imagine that the text books represent the unchanging text portion of the 
program and the notebook to represent the changing data which might make 
things a little clearer.
It is the responsibility of both the kernel and the CPU to ensure that I don't 
end up reading someone else's textbook or writing in someone else's 
notebook. That is, both the kernel and the CPU ensure that one process does 
not have access to the memory locations of another process (a discussion of 
cell replication would look silly in my calculus notebook). The CPU also 
helps the kernel by recognizing when the process tries to access a page that is 
not yet in memory. It is the kernel's job to figure out which process it was, 
what page it was, and to load the appropriate page. 
It is also the kernel's responsibility to ensure that no one process hogs all 
available memory, just like the librarian telling me to make some space on the 
table. If there is only one process running (not very likely), there may be 
enough memory to keep the entire process loaded as it runs. More likely is 
the case in which dozens of processes are in memory and each gets a small 
part of the total memory. (Note: Depending on how much memory you have, 
it is still possible that the entire program is in memory.)
Processes generally adhere to the principle of spatial locality. This means that 
typically processes will access the same portions of their code over and over 
again. The kernel could establish a working set of pages for each process, the 
pages that have been accessed with the last n memory references. If n is 
small, the processes may not have enough pages in memory to do their 
job.Instead of letting the processes work, the kernel is busy spending all of its 
time reading in the needed pages. By the time the system has finished reading 
in the needed pages, it is some other process's turn. Now, some other process 
needs more pages, so the kernel needs to read them in. This is called 
thrashing. Large values of n may lead to cases in which there is not enough 
memory for all the processes to run.
The solution is to use a portion of hard disk as a kind of temporary storage for 
data pages that are not currently needed. This area of the hard disk is called 
the swap space or swap device and is a separate area used solely for the 
purpose of holding data pages from programs. 
The size and location of the swap device is normally set when the system is 
first installed. Afterward, more swap space can be added if needed. (Swap 
space is added with the mkswap command and the system is told to use it 
with the swapon command.) 
Eventually, the process that was swapped out will get a turn on the CPU and 
will need to be swapped back in. Before it can be swapped back in, the 
system needs to ensure that there is at least enough memory for the task 
structure and a set of structures called page tables. Page tables are an integral 
part of the virtual memory scheme and point to the actual pages in memory. I 
talk more about this when I talk about the CPU in the hardware section. 
Often you don't want to swap in certain pages. For example, it doesn't make 
sense to swap in pages for a process that is sleeping on some event. Because 
that event hasn't occurred yet, swapping them in means that it will just need to 
go right back to sleep. Therefore, only processes in the TASK_RUNNING 
state are eligible to have pages swapped back in. That is, only the processes 
that are runnable get pages swapped back in.
Keep in mind that accessing the hard disk is hundreds of times slower than 
accessing memory. Although swapping does allow you to have more 
programs in memory than the physical RAM will allow, using it slows down 
the system. If possible, it is a good idea to keep from swapping by adding 
more RAM. 




--    synco gibraldter
--    atlanta, ga
--    synco at xodarap.net
--    key id: 0xC5117E0A
-------------- next part --------------
An HTML attachment was scrubbed...




More information about the Ale mailing list