Tuesday, 14 May 2013

Difference between Swapping and Paging in linux

Difference between Swapping and Paging in linux

Swapping:       
Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.
Let's say you start ten heavyweight processes (for example, five xterms, a couple netscapes, a sendmail, and a couple pines) on an old 486 box running Linux with 16MB of RAM. Basically, you *do not have* enough physical RAM to accommodate the text, data, and stack segments of all these processes at once. Since the kernel cannot find enough RAM to fit things in, it makes use of the available virtual memory by a process known as swapping. It selects the least busy process and moves it in its entirety (meaning the program's in-RAM text, stack, and data segments) to disk. As more RAM becomes available, it swaps the process back in from disk into RAM. While this use of the virtual memory system makes it possible for you to continue to use the machine, it comes at a very heavy price. Remember, disks are (by the factor of a million) than CPUs and you can feel this disparity rather severely when the machine is swapping. Swapping is not considered a normal system activity. It is basically a sign that you need to buy more RAM. 
In Unix SVR4, the process handling swapping is called sched (in other Unix variants, it is sometimes called swapper). It always runs as process 0. When the free memory falls so far below minfree thatpageout is not able to recover memory by page stealing, sched invokes the syscall sched(). Syscallswapout is then called to free all the memory pages associated with the process chosen for being swapping out. On a later invocation of sched(), the process may be swapped back in from disk if there is enough memory.

Paging:          
Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory.
When a process starts in Unix, not all its memory pages are read in from the disk at once. Instead, the kernel loads into RAM only a few pages at a time. After the CPU digests these, the next page is requested. If it is not found in RAM, a page fault occurs, signaling the kernel to load the next few pages from disk into RAM. This is called demand paging and is a perfectly normal system activity in Unix. (Just so you know, it is possible for you, as a programmer, to read in entire processes if there is enough memory available to do so.)
The Unix SVR4 daemon which performs the paging out operation is called pageout. It is a long running daemon and is created at boot time. The pageout process cannot be killed. There are three kernel variables which control the paging operation (Unix SVR4):
  • minfree - the absolute minimum of free RAM needed. If free memory falls below this limit, the memory management system does its best to get back above it. It does so by page stealingfrom other, running processes, if practical.
  • desfree - the amount of RAM the kernel wants to have free at all times. If free memory is less than desfree, the pageout syscall is called every clock cycle.
  • lotsfree - the amount of memory necessary before the kernel stops calling pageout. Betweendesfree and lotsfreepageout is called 4 times a second.

2 comments:

  1. Hello Jitendra,


    Jeez oh man, while I applaud for your writing , it’s just so damn straight to the point Difference between Swapping and Paging in linux.


    I have a net book Samsung N150plus,and sometimes I'm having problems with google that crashes and close, even is a bit slow as I'm going to a 2gb ram also, I had re installed it several times but someone told me to install a Linux software on my net book,
    can i?are there any disadvantages?
    Please keep providing such valuable information.


    Obrigado,
    Abhiram

    ReplyDelete