January 8th, 2006, 02:01 AM
Preventing a Swap Market Crash
Preventing a Swap Market Crash
Hopefully our minds are not thinking about wild parties when we mention swapping. But rather about moving something we haven't used in a while to a different location until it's needed again, thus freeing up space for things we are going to use right away. In essence that is a definition, albeit coarse, of the Linux Virtual Memory System. It functions automatically when the capacity of the real (physical) memory is exceeded. Inactive memory pages are shifted to the swap partition or file, in exchange for active pages.
Unlike like Linux some other Operating Systems allow software to control the size of their virtual memory. Whereas we find that Linux Users predominantly employ a pre-established/determined size for its virtual memory. There are however, active discussions and programs on the ready for establishing a dynamic swap environment for Linux.
Additionally if you didn't already know, the rate of swap as it relates to performance, is also a current topic of discussion. In the past, that was static as well. It should be noted that the "swapiness" (rate of exchange) on the 2.6 Kernel can be adjusted. From the Linux Kernel Mailing List (lkml):
Another brief discussion on the lkml looked into the complaint that a Linux server can occasionally bog down paging data in and out of swap. This time, the proposed solution was the usage of a userland dynamic swap creation daemon, such as swapd or dynswapd. The latter's home page explains, "dynamically de/activates swap files and partitions as required, creating and deleting swap files as necessary." Unfortunately, as Paul Jackson points out, there is little gained for the average user as hard drive space is inexpensive and any amount of swap can usually be allocated permanently.
In other words, the expensive part of swap is when reading from it or writing to it. For those using the 2.6 kernel, "swappiness" can be tuned [story] by modifying /proc/sys/vm/swappiness. The variable can be set from 0 to 100, with a lower number telling the kernel to swap less.
As indicated in the quote above, there is very little to be gained in dynamically de/activating swap space for the average user. Therefore establishing a swap partition or file of fixed size may be prudent. Also determining and establishing the right size will definitely assist in Preventing a Swap Market Crash. How can you crash the swap exchange?
If you fail to allocate enough space you could actually run out and well...things will probably slow down to an absolute crawl or even come to a screeching halt. However on the other hand, if you allocate too much and actually use it all, you could possibly create a condition known as thrashing. By definition, that is a serious performance problem wherein your system will spend all of its time swapping! Affectionately known as "Swapping Hell". I have never encountered thrashing as they have described it, so I did say "could" for a reason. One source indicates that you will likely suffer the condition if you create too large of a virtual memory, and another source says it will not cause the system to exhibit thrashing or otherwise cause operational problems (I did provide a couple of other definitions at the end for comparison.). So minus a definitive answer, a maybe is sufficient for now. At the least if you are resource limited, the excessive swap space could be better utilized.
Regardless, what is the answer to the "How much space should I allocate for a swap partition or file question?"
As much as I love absolutes, one does not exist in this case. Conventional wisdom from many a brain housing group will usually state that you should use two times your real (physical) memory. Although that recommendation almost always seems to provide sufficient virtual memory (real memory + swap), that wisdom actually originated in the way back. The when being: when we thought 8MB of Ram was the ultimate state. The emphasis during that era was to create a virtual memory condition of somewhere between 30-40 MB.
With the advent of the massive storage capacity of our current hard disks and the density of our modern memory sticks, how much should you allocate? In my scenario, I am not a gamer or a graphics engineer, however I do play with C & C++. So how much would I use? I have 1.0GB of physical memory for my dual-booting SuSE & XP Box. Do I really need 3.0GB of virtual memory (1.0GB real memory + 2.0GB swap partition) for my applications? No, probably not. In fact in this same scenario SuSE suggests that between 256 and 512MB would be more than adequate. However since I have the hard disk space available I allocated 1.0GB for the swap partition thus creating a virtual memory of 2.0GB. In my case, it is most unlikely that I will ever dip into the swap partition (unless I borrow someone's Doom 3 that was ported for Linux), however the swap space is available for future growth. So at least in this situation, the two times the ram is most likely over kill.
A key point to remember is that the originator's normal install back then was probably much smaller than a stripped down install of our modern versions. Obviously they didn't have the huge storage available on our hard drives or memory sticks coming in at 1.0GB either. So two times the memory was quite appropriate then.
So where are we today? Modern wisdom indicates that you should analyze what you will be using the computer for, your current configuration; and if appropriate, set aside two to five times your real (physical) memory for swap.
The next question is obviously, where should we allocate this swap space? We actually have a couple of choices. I'll just list the two familiar ones.
Swap Partition vs Swap File
I would imagine the normal Linux user associates the term "Swap" with a partition, and it's probably pretty safe to say that most folks created a Swap Partition during the installation process; whether you use the default install or custom partitioning. However, you can also use a regular file as swap space. It won't be as fast as fast as a separate swap partition because as a file, it will obviously be part of the file system. However if you are in a situation where you would have to repartition to continue, using a file as additional swap space may be the answer you are looking for. Here are some definitions and the commands that will be required:
swapon - This command will display the swap usage or in the event swap is off, it will direct the system to start using the swap device or file.
swapoff - This command will terminate swap use.
dd - This command will convert and copy data. In this particular case we are using the "dd" command to create a file, specifically a "swap_file", with a block size of 1024k bytes and a count (desired size you specify) in MBs. If you are not familiar with this command you can make some practice trials before hand. However, obviously you don't want to name the new files you create, "swap".
mkswap - This command will set up a Linux swap area on a device or in a file. In our use below, it will be "swap_file".
For a full description of the commands call up a terminal and access the manuals. (i.e. Relyt@play:# man mkswap). The manual will provide you will all the options to the various commands. So putting it all together, call up a terminal/command prompt and then type:
dd if=/dev/zero of=swap_file bs=1024k count=num_mb
As you can see we activated the new swap space by using the "swapon" command. If later you determine that you no longer require the new swap file, use the "swapoff" to stop its use. The file will remain in the event you need it once more.
The procedures for creating a "swap partition" has been previously covered a multitude of times here on AO and the Internet so I'll pass on that.
If some folks have had real life experiences with thrashing, please feel free to comment and provide some insight about how it happened and what you did to eliminate it. Enjoy. And finally, for your viewing pleasure:
"In systems that use virtual memory, the resulting condition of a hard drive being used excessively for virtual memory because the physical memory (i.e., RAM) is full. (The process of moving data into and out of virtual memory also is called swapping pages.) Disk thrashing considerably slows down the performance of a system because data has to be transferred back and forth from the hard drive to the physical memory." Source
"Thrashing is computer activity that makes little or no progress, usually because memory or other resources have become exhausted or too limited to perform needed operations." Source
Connection refused, try again later.