# Should I drop my swap?

## JonnoG

Hey, a week or so ago I finally got around to throwing up a little conky config. I've had my eye on my memory and swap since, and I've never seen swap used at all. I guess because I never get close to full memory, which is a bit of a waste I suppose.

I'd read around a fair bit when I installed the system for a good size for my swap, I have 4gb of RAM and ended up settling with a 6gb swap partition.

I have a terabyte of space so it's not a massive chunk, but it still seems it could be put to better use.

So, I've been running today with swapping disabled and trying to abuse the system as much as I can, but I haven't noticed any performance issues, so is it worth getting rid of it altogether? And is there any way I'm going to regret doing it?

TIA, Jonno.

----------

## isilia

You probably don't need the swap space as storage space or as additional memory, so it does not matter what you do with it. I don't use swap, though I have 4GB of RAM, and I only went OOM (out of memory) once, when I was running a MMO video game and 2 virtual boxes (I could've set the ram options a bit more restrictive though). So personally I find swap memory a bit outdated/useless for modern desktop systems with plenty of RAM. But then again I got 1 TB of free diskspace, too.Last edited by isilia on Sat Sep 26, 2009 2:26 am; edited 2 times in total

----------

## JonnoG

Thanks, it's good to know I probably wont miss it if I ever somehow get to the point that I need an extra 6 gig disk space  :Razz: 

----------

## energyman76b

NO! Don't remove swap!

Even if you don't need it - you do need it  :Wink: 

Without swap the kernel can not overcommit and without swap processes might screw you up. So... just leave it there. It is not like you need that 6gb do you?

----------

## Maf

 *energyman76b wrote:*   

> Without swap the kernel can not overcommit and without swap processes might screw you up. So... just leave it there. It is not like you need that 6gb do you?

 

Could you tell something more about it? I haven't use swap for like a year and I guess everything is working ok.

----------

## energyman76b

overcommiting is giving a process more memory then there is. Sometimes a stupid process asks for an insane amount that the kernel can not satisfy with 'normal' memory - but with swap it can. The process will never use that much memory, so swap is not touched and nobody is hurt. 

If you remove swap, kernel can not do that. Process fails. Program might crash.

If your system is free of stupid stuff, congratulation  :Wink: 

----------

## Simba7

Why use 6GB of Swap? I'd chop it down to a gig or 512MB.

In my router, I have 640MB of RAM, but 320MB of Swap.

----------

## toralf

AFAIK suspend2disk needs a separate (swap) partition, isn't it ?

----------

## sera

 *energyman76b wrote:*   

> Without swap the kernel can not overcommit and without swap processes might screw you up. So... just leave it there. It is not like you need that 6gb do you?

 

The kernel can overcommit whether there is swap or not.

----------

## NeddySeagoon

JonnoG,

Linux swapping comes in several forms. The swap partition is used to house allocated RAM that has no permanent home on disk.

If you remove swap, you deny the kernel that swap option. It will still flush and reload code and data from disk if it needs to. 

Keep your swap but 6G is over the top.

----------

## energyman76b

 *sera wrote:*   

>  *energyman76b wrote:*   Without swap the kernel can not overcommit and without swap processes might screw you up. So... just leave it there. It is not like you need that 6gb do you? 
> 
> The kernel can overcommit whether there is swap or not.

 

sure, he can overcommit - until the process needs the memory. Then you go down.

----------

## widremann

 *energyman76b wrote:*   

>  *sera wrote:*    *energyman76b wrote:*   Without swap the kernel can not overcommit and without swap processes might screw you up. So... just leave it there. It is not like you need that 6gb do you? 
> 
> The kernel can overcommit whether there is swap or not. 
> 
> sure, he can overcommit - until the process needs the memory. Then you go down.

 

But the process doesn't need the memory, that's the thing.  Otherwise, it wouldn't be overcommitting.

I think you misunderstand when swap is actually used.  It's only used when dirty pages in RAM need to be evicted to make room for new pages, or pages brought it from disk that a process needs, i.e., for pages that are already being used.  If a process allocates a huge chunk of virtual memory and only uses a small part of it, then no page frames or swap frames are ever used for the unused portion.

Furthermore, if you have no swap, that doesn't mean the kernel can't swap out clean pages to disk.  Swap is only necessary for anonymous memory (e.g., heap segments, the stack, etc.).  Any page frames that are backed by a real file, such as code segments, read-only data segments, mmap()ed files, etc., can either be discarded, or flushed back to disk to the files that they are backed by.  No swap space needs to be used.

So, the point is that you only ever need swap space if you have more dirty anonymous memory pages than can fit into RAM and there is memory pressure to allocate more page frames.  Outside of that scenario, swap will never have to be used.

The downside to no swap is that the kernel may optimistically swap out anonymous pages to the swap partition if they haven't been used in a while under the assumption that they won't be used for a while again either, and so more important things can be brought into RAM.  This also allows the file cache to stay big and healthy, which is very important for good performance (unless you have an SSD, I guess).

----------

## zyko

There is a compromise that gains you some flexibilty: Get rid of your dedicated swap partition. That's definitely a huge waste.

Instead, you can use a swap file anywhere on your file systems. This would allow you to adjust the size of your swap space on the fly or to delete swap altogether without having to repartition.

Example (howto create a 256MB swap file /swappy): 

```
# touch /swappy

# dd if=/dev/zero of=/swappy bs=1M count=256

# mkswap /swappy

# swapon /swappy
```

You may also want to restrict read access to root only:

```
# chmod 0600 /swappy
```

Don't forget to adjust /etc/fstab.

You'll probably find out that you never ever eheheheeeever use even a single bit of swap anyway.

----------

## pdw_hu

 *toralf wrote:*   

> AFAIK suspend2disk needs a separate (swap) partition, isn't it ?

 

It can work perfectly fine with a swap file too. No need to waste a partition on it.

Just dd yourself an appropriate size, mkswap /where/file/is, and mount it as swap. Voi'la.

----------

