I am using RAM for storing some of my database tables and the others are stored in hard disk.
Today I came to know that my processes are using swap memory. Now what is swap memory and how can I detect that which process is using swap memory and how can i stop them from using it?
If you run out of physical memory, you use virtual memory, which stores the data in memory on disk. Reading from disk is several orders of magnitude slower than reading from memory, so this slows everything way down. (Exchanging data between real memory and virtual memory is "swapping". The space on disk is "swap space".)
If your app is "using swap", then you either need to use less memory or buy more RAM.
(Swap is useful because applications that aren't being used can be stored on disk until they are used. Then they can be "paged in" and run normally again. While it is not in memory, though, the OS can use that memory for something else, like disk cache. So it's a very useful feature, but if you don't have enough physical memory to run your program, you definitely need more memory. Fortunately, memory is really really cheap these days.)
A computer has a certain amount of physical memory. But most of the time we want more. So we swap some memory on disk.
Each time we need that memory it needs to be swapped back into memory (swapping some other piece out). There are extremely intelligent algorithms to minimize the delay, but still sometimes we have to wait.
On windows, if you look at the task manager, you can show the column page faults. This shows the number of time, an application asks for a piece of memory that was on the disk and needs to be swapped. Lots of page faults = slow application.
This process is used on several levels. A summary from fast to slow:
If you don't want to use swap, then don't use a swap partition (comment out the line in
/etc/fstab
), but that will prevent to start new process and will make malloc fail if you run out of physical memory. That's not a good idea in general... not very safe and not even very useful, as the OS will keep the hot pages in memory and will swap to disk those that are not being used recently.Another solution could be to hack your the database so it locks the pages into memory (I know that can be done, but I couldn't tell you now exactly how), but anyway, it's likey that your database will use it's own swapping and caching mechanisms.
To know what is the swap and how the virtual memory works... I'd recommend you to take a look to some Operating System lectures, maybe the MIT Open Courseware could be of help.
I have found a partly answer. A Swap Memory is a space in the Hard Disk of your computer that Operating Systems will use to put the info that is actually on the RAM to free it for another application. This should be done when the system needs memory for a new process and there is none, so we can see that if our system has plenty of RAM it will maybe need no SWAP memory.
UPDATE
To find out where Swap space (or virtual memory) you can go to Task Manger (CTRL+ALT+DEL) and turn on from VIEW -> COLUMNS -> VIRTUAL MEMORY. Now just calculate the difference between memory usage and virtual memory. Also here is an instruction how to see it if you are running Vista: http://www.ehow.com/how_5067693_increase-swap-space.html
In both Linux and Windows, there will be dedicated RAM. RAM has an insufficient amount of memory to hold a process, it borrows some amount of memory from the secondary storage. In this way, the RAM finds sufficient space to hold a new process within it. Hence, the borrowed space from the hard disk is called Swap Memory. A swap file is a system file that creates temporary storage space on an SSD or HDD when the system runs low on memory.
For clear swap memory
sudo swapoff -a
sudo swapon -a
Hope it will help you. Thanks
Swap is hard disk space used as RAM. It is (relatively speaking) very slow, but stops computers from crashing when they are trying to deal with more data then their RAM can handle.
To stop processes from using swap — install more RAM.
If you do not want your processes to use swap-memory, just disable swap memory altogether. Unfortunately, if you ever run out of memory, the Linux kernel will kill some of your running processes in order to free up some memory for use.
If you have sufficient RAM for your system, you can actually go without swap memory.
I have a laptop with 768Mb of RAM that does not use swap memory. It is good enough for my needs. I have a server with 256Mb of RAM and does not use swap memory. It is running 8 virtual servers using OpenVZ virtualisation, without problems either.
So, it is up to you if you need swap-memory.