LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Adding SWAP Partition (http://www.linuxquestions.org/questions/linux-general-1/adding-swap-partition-4175466255/)

ej_219 06-16-2013 05:30 PM

Adding SWAP Partition
 
Talked my boss into installing Ubuntu on a desktop at work, XP kept locking up with BSOD. The desktop has 1GB ram and the SWAP partition is currently 1GB. PC is running very slow and I was thinking additional SWAP space would improve performance. Could anyone please explain the best way to add a SWAP partition to this desktop? I did find one method on thegeekstuff.com from 2010, URL attached. Would this a good method to try, or is there a better way? Thanks from a Linux nooby.

http://www.thegeekstuff.com/2010/08/...dd-swap-space/

evo2 06-16-2013 08:49 PM

Hi,

welcome to LQ. Before considering adding more swap you should check that you actually need it. You should use the "free" command. To understand the output, have a look at www.linuxatemyram.com

HTH,

Evo2.

DOSJockey382 06-16-2013 09:06 PM

First of all, have you identified that the system is using swap space in the first place? Unless you know you are hitting your swap cap, you probably do not need to increase swap space. Yes, hitting your swap cap will slow a computer down immensely. However, using swap space in and of itself will slow your computer down. You are going from RAM speed to HDD speeds. Even SSDs with swap will slow you down a little, though much less so than HDDs. You may wish to run TOP or some other program that tracks real-time memory usage (RAM and SWAP) to know exactly where the speed bottleneck is happening and if it is specifically memory related.

Now let's assume that the problem is a need for more swap space. I have heard different variations on what I am going to say next, but my general rule of thumb when it comes to swap space give swap 3x RAM size. But, should you use those instructions to increase the swap space? Don't know. I try to set my partitions correctly (or what I think is correct) before I even get to installing a system. I am not a big fan of dynamically altering partitions on installed systems, though I know very well it can be done. Too many things can go wrong that will force you to repartition and reinstall the system fresh anyway. But feel free to try it.

One last thought, If memory limitations are the problem, might want to consider increasing the RAM on the computer.

chrism01 06-16-2013 11:49 PM

Definitely check to see if you NEED more swap before bothering.
Read the link provided by evo2 and run the 'top' cmd to see what's really happening.

Firerat 06-17-2013 12:29 AM

As a general rule
more swap will allow you to run more programs, slower and noisier ( disk thrashing ).


For a better experience, with just 1gb ram, look at lubuntu or xubuntu

johnsfine 06-17-2013 01:26 AM

Quote:

Originally Posted by ej_219 (Post 4973049)
PC is running very slow and I was thinking additional SWAP space would improve performance.

It is very unlikely that additional SWAP space would improve performance.

But answering your question anyway.

Quote:

Could anyone please explain the best way to add a SWAP partition to this desktop?
It is better to expand an existing swap partition than to add another.

You would boot in a liveCD (such as the install media) and use a GUI partitioning tool (such as gparted). You probably need to shrink some other partition in order to expand swap.

The liveCD boot might have enabled use of the swap for itself. If so, you need to disable that before using gparted. In command line that is:
/sbin/swapoff -a

Adding a swap file is easier than adding swap partition or expanding a swap partition. That can be done without rebooting.

Quote:

I did find one method on thegeekstuff.com from 2010, URL attached. Would this a good method to try, or is there a better way?
For adding a swap file, that looks right.
For adding a swap partition, that looks plausible for the correct second half of the task. You need to use a partitioning tool for the first part (actually creating the partition). Probably that would need rebooting in liveCD and first shrinking some other partition. By the time you do all that, it is easier to expand the existing swap than to add a new one.

Quote:

Originally Posted by DOSJockey382 (Post 4973143)
First of all, have you identified that the system is using swap space in the first place? Unless you know you are hitting your swap cap, you probably do not need to increase swap space.

That is the important answer for this thread (same as evo2 and others said).

Quote:

Yes, hitting your swap cap will slow a computer down immensely.
Lack of swap space can slow a computer down. But "immensely" is very unlikely. Even if all the allocated swap space is used, lack of more swap space probably only slows the computer a little.

Quote:

Even SSDs with swap will slow you down a little, though much less so than HDDs.
If you actually need swap space, having it on SSD is inferior to having it on HDD. SSD is good for uses that are mostly read, not uses (such as swap) that are as much write as read. But I expect the computer discussed in this thread has no SSD, so this side topic is irrelevant.

Quote:

my general rule of thumb when it comes to swap space give swap 3x RAM size.
No such rule of thumb gives you good guidance on a good choice of swap size. In modern systems, there is no correlation between ram size and appropriate swap size. So all the various 1x or 2x or 3x "rule of thumb" suggestions are misleading.

If you need a blind "rule of thumb" (independent of looking at what you actually need) suggestion for swap space, you'll do better with mine: Swap space should be 2GB regardless of ram size (but less if you are very short of disk space).

vrrivaro 06-18-2013 01:55 PM

Adding swap space is valid!
 
While what most have previoualy said here is true (not all--there is zero risk involved), there is definetly an excellent reason for adding swap space to a machine. If Linux required a total reinstall to add it would be absolutly Stupid(TM)...

For starters, you might have added memory to the machine. Since the formula for the amount of swap recomended changes depending on the main memory size, it now has too little swap.

Secondly, you might have done a miscalculation when doing the instalation.

Third, your requirements might have changed.

Now, let me defend the 2x or 3x postures...

Linux uses the standard swap space for the "Suspend to Disk" feature. Therefore, if your free swap space is less than your physical ram plus the currently used swap memory, the "suspend to disk" operation will fail. As a result, your notebook's battery could drain out and you could loose important data. Keep that in mind when calculating the swap space. It s better to have unused swap space, than not enough...

Now, how do I go about dinamically adding swap space? Say you want to add 16 GB:

$ sudo -i
# dd if=/dev/zero of=/root/swapfile bs=$((1024*1024)) count=$((16*1024))
# mkswap /root/swapfile
# swapon /root/swapfile
Now, to make it permanet, you simply need to add that last line anywhere into the /etc/rc.local file, as long as it is before the "exit" at the end.

A comment on the dd command. It copies an endless stream of zeroes (/dez/zero) into our target directory in blocks (bs) of 1 MB (the amount in parenthesis) and exactly 16K of them. The result is a string of zeroes filling 16GB into the file /root/swapfile.

Edited for clarification:
  1. I used the bash construct $((...)) but forgot to explain it. It simply means to perform the math enclosed and substitute in the result. If instead you typed "dd if=/dev/zero of=/root/swapfile bs=1048576 count=16384", you would get the exact same result.
  2. The parameters to the dd command are not magical. This could get the exact same result:
    $ sudo -i
    # INPUT_FILE="/dev/zero"
    # OUTPUT_FILE="/root/swapfile"
    # BLOCK_SIZE=$((1024*1024))
    # BLOCK_COUNT=$((16*1024))
    # dd if=$INPUT_FILE of=$OUTPUT_FILE bs=$BLOCK_SIZE count=$BLOCK_COUNT

I do hope I have made myself clearer this time.

ej_219 06-26-2013 07:34 PM

Thanks for all the responses. Sorry I haven't gotten back sooner, busy work schedule. When I checked the swap space using free, on one occasion the swap was almost maxed and on another occasion it was using less than half of the space available. I normally set the swap size at twice the size of memory, with a 2GB max. My boss is an old IT guy (new to Linux) and installed Ubuntu over a weekend and set the swap at 1GB, same size as memory. When I get some slow time @ the job I will use DOSJockey382's recommendation of Top to see just what is slowing things down. If I do need to increase swap size I will try the script by vrrivaro.
Thanks again for all the advice.

chrism01 06-26-2013 08:52 PM

These days I'd go with min 2GB swap, even on a desktop. GUIs use a lot of RAM and diskspace is cheap; give it 4GB if you feel like it.

TobiSGD 06-26-2013 09:23 PM

Quote:

Originally Posted by vrrivaro (Post 4974318)
For starters, you might have added memory to the machine. Since the formula for the amount of swap recomended changes depending on the main memory size, it now has too little swap.

Old time rule of thumb that is invalid for a running system. You are right that the amount of swap needed is determined by the amount of RAM when you want to use hibernating (suspend-to-disk), but it is not dependent on the amount of RAM for a running system per se.
The amount of swap needed is determined by the amount of RAM you have and the workload of the machine. If you never use all of the physical RAM adding more RAM will not change anything for the amount of swap needed. So the first thing to do is to check if your workload even hits the swap. If it does hit the swap you will still not get better performance with increasing the amount of swap, you will just get more room before the OOM killer is triggered. There is only one way to increase performance in that case: Add more physical RAM.
Or maybe, if you have the chance, decrease the memory load (with using lightweight software, for example, as mentioned already, using Lubuntu instead of Ubuntu).

sundialsvcs 06-27-2013 10:04 AM

When you're looking for quick-fixes, "chips are cheap." Find out how much memory the system will hold, and go buy it. You'll almost always be glad that you did.

However, "slow performance" really does require digging a little deeper. If the system is "perceptibly slow," look to see if CPU utilization is "quite low." Usually it is, which means that processes are being forced to wait. High disk-activity is your easiest cue that "thrashing" due to memory over-utilization might be the problem: if one program is consuming too much memory, perhaps it has a configuration parameter that you can adjust, or you can impose a limit with ulimit.

Sometimes it turns out to be the GUI that is "slow," perhaps due to less-than-optimal settings concerning the video card, and so forth. Anything that serves as a "common choke-point" shared by many programs at the same time can have an unexpectedly-large influence on perceived performance ... and, when it comes to performance, "perception is everything."

jefro 06-27-2013 03:01 PM

An old xp box that locked up in xp may or may not be a good choice for a modern distro like ubuntu.

If only slow then maybe try some lighweight distros for testing.


All times are GMT -5. The time now is 04:16 PM.