Concerned About Too Much Swap Memory Use on Linux Ubuntu?
As I was staring at the solidly lit hard disk LED indicator, I realized that the system, a Dell Latitude D620 running Linux Ubuntu 10.04 Lucid Lynx, was spending to too much time accessing the slow hard disk rather than keeping the data in RAM memory. As taught in any Introduction to Computer Science 101 class, the magnetic hard disk reads and writes data at the speed range of a few megabytes (mb) per second, while electronic random access memory (RAM) does reads and writes in the hundreds of mb per second range. Therefore, I lowered the amount of data or programs stored on my hard drive by executing the following command at the system prompt:
~$sudo sysctl -w vm.swappiness=10
The default swappiness value is 60.
You check your swapiness value using:
~$cat /proc/sys/vm/swappiness
I put the code at the end my /etc/sysctl.conf file so that it will boot up with the lower swapiness value. If you have a computer with low memory, like less than 1gb, then you should probably leave it at 60. If your system is freezing or crashing because of insufficient memory then you could set it to 100, however, your machine will run at a snail’s pace.
To take advantage of the new setting I flushed the swap data from the hard drive to memory by executing the flowing program at the command prompt. You could put these statements in to BASH script to automate this process.
~$sudo swapoff -a
I turned on the swap process back with:
~$sudo swapon -a
Although it is not necessary to flush un-used data and programs from volatile memory, because the system keeps track of available space anyways, you can do it by running the following program at a terminal’s system prompt:
~$sudo echo 3 > /proc/sys/vm/drop_caches
The above command really is not required as the system keeps the most recently used data or software in RAM in case you or it needs to use it again and it will be available to use almost instantly, speeding up your tasks or the system’s tasks most efficiently. Tools like free:
~$free -t -m
total used free shared buffers cached
Mem: 1498 833 664 0 50 318
-/+ buffers/cache: 464 1033
Swap: 0 0 0
Total: 1498 833 664
or, top or system monitor the will show a high “used” amount of RAM, that includes data and programs no longer in use but that might be used in the future.
top – 15:12:47 up 23:31, 2 users, load average: 0.94, 0.81, 0.77
Tasks: 182 total, 1 running, 181 sleeping, 0 stopped, 0 zombie
Cpu(s): 8.7%us, 8.7%sy, 0.0%ni, 80.4%id, 0.0%wa, 2.2%hi, 0.0%si, 0.0%st
Mem: 1534556k total, 866732k used, 667824k free, 51820k buffers
Swap: 0k total, 0k used, 0k free, 338300k cached
Bookmark with:
Digg
reddit
Facebook
StumbleUpon