50

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?

030
  • 5,731
  • 12
  • 61
  • 107
  • isn't there any way to find out that which processes are using swap memory??? –  Jul 31 '09 at 08:52
  • look at the windows task manager (or similar tools) for the amount of page file used and page faults. –  Jul 31 '09 at 08:54
  • 1
    Swap is not a per-process thing, it's for the entire OS. "vmstat" will show you information about how much swap you are using. – jrockway Jul 31 '09 at 08:54
  • i work on linux not on wimdows –  Jul 31 '09 at 08:57
  • got the reason of this swap usage.....someone was trying to parse up my site thats why the whole thng ws happening without any cause from our side.Thanks all for your answers it helped me lot as always :-) –  Jul 31 '09 at 09:22

7 Answers7

65

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.)

jrockway
  • 859
  • 5
  • 4
  • 1
    For the sake of correctness: virtual memory means using virtual memory addresses (something that is always enabled in a modern CPU, no matter how many available RAM you have) and is not directly related to swapping (although they are used in combination, they are basically different concepts). – fortran Jun 22 '12 at 11:24
  • @fortran, for double correctness, virtual memory is [the term that Windows uses when it refers to the paging (swap) file](http://windows.microsoft.com/en-us/windows/change-virtual-memory-size#1TC=windows-7). – sleblanc Jan 16 '16 at 05:38
8

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:

  • On the CPU level, there are registers. These are the fastest memory but the amount is limited.
  • Also on the CPU there is a small memory cache. Here a small piece of the running program is stored for fast access. Search on branch predicting algorithms if you want to know more.
  • Sometimes there are level 2 caches between the CPU and the main memory.
  • Next level is the main memory (RAM).
  • Last level and slowest of all is the disk, sometimes you can use USB sticks as extra memory.
Lekensteyn
  • 6,111
  • 6
  • 37
  • 55
Toon Krijthe
  • 271
  • 2
  • 4
4
cat /proc/pid/smaps

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.

fortran
  • 151
  • 4
  • Since the default config on many linux distros is to overcommit memory, the "worst" that will happen is that the OOM killer will start sniping processes, most likely starting with the DBMS (and with tables "stored in RAM" that's probably not a good thing). – symcbean Oct 05 '16 at 18:36
1

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

faya
  • 111
  • 2
0

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

sudo free –h
sudo shutdown -r now

Hope it will help you. Thanks

-1

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.

Quentin
  • 1,117
  • 6
  • 10
  • 1
    If you don't want any swapping, stop using a page file. But expect other problems. –  Jul 31 '09 at 08:55
  • 1
    It's not. Swapping is moving a block of data _from_ the harddisk into physical memory. A one-time read, which is normally fast enough. –  Jul 31 '09 at 09:03
  • @xtofl the answer didn't mention swapping, just swap partition, and the comment by gamecat didn't have anything to do with what you said? :P – RapidWebs Jul 17 '14 at 05:58
-1

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.

sybreon
  • 7,357
  • 1
  • 19
  • 19
  • *HOW* are you running **8** VMs on a machine with only 256 ***megabytes*** of RAM?!? – warren Jun 28 '11 at 20:27
  • It's possible with VZ because the RAM is not completely partitioned as common libraries are shared across VMs, or something that effect. So, 8 x 64M is not necessarily 512M. :) – sybreon Jun 29 '11 at 11:53
  • nonetheless - how is the underlying device running stably? – warren Jun 29 '11 at 12:17
  • By swapping out inactive programs, you have more memory for file caching. And that speeds things up. this one time read is usually fairly quick and has negligible effect overall. it is not just used when you go out of bounds within regard to your available RAM. I have seen machines with 16GB of RAM use SWAP at times. – RapidWebs Jul 17 '14 at 06:22
  • moreover, I wouldn’t recommend to disable SWAP on a Database server. His customers or web-app would not be very pleased when OOM kills the back-end all over a simple nightly backup operation, or something similar? it would be more intelligent to tune the database cache in relation to application usage and available memory. if he is already swapping, he either needs more ram, or to configure his DB server better. otherwise, disabling SWAP would coerce OOM to kill the DB server every time it goes to swap. which judging by his question, is often. – RapidWebs Jul 17 '14 at 06:24