Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
isn't there any way to find out that which processes are using swap memory??? –  developer Jul 31 '09 at 8:52
    
look at the windows task manager (or similar tools) for the amount of page file used and page faults. –  Gamecat Jul 31 '09 at 8:54
    
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 8:54
    
i work on linux not on wimdows –  developer Jul 31 '09 at 8: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 :-) –  developer Jul 31 '09 at 9:22
add comment

migrated from stackoverflow.com Jul 31 '09 at 11:20

This question came from our site for professional and enthusiast programmers.

6 Answers

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.
share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

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

share|improve this answer
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
add comment

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.

share|improve this answer
1  
If you don't want any swapping, stop using a page file. But expect other problems. –  Gamecat Jul 31 '09 at 8: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. –  xtofl Jul 31 '09 at 9:03
add comment

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

share|improve this answer
add comment
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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.