Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

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.

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.

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

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

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:

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:

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.

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.

Last edited by vrrivaro; 06-29-2013 at 08:27 PM.
Reason: Added some comments for clarification purposes

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.

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

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