{"id":369,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:38","modified_gmt":"2020-08-22T20:26:38","slug":"this-is-the-page-title-toplevel-202","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=369","title":{"rendered":"Swap Space"},"content":{"rendered":"\n<title>Swap Space<\/title>\n<p class=\"subtitle\">Swap Basics<\/p>\n<concept id=\"\" description=\"If you expect to be adding RAM after you install your system, you should consider making your swap space larger.\" \/>\n<question id=\"\" type=\"MC\" text=\"What was the traditional 'rule of thumb' for the size of your swap space?\" \/>\n<question id=\"\" type=\"TF\" text=\"Increasing swap may require reinstalling the operating system.\" \/>\n<p>\nThe traditional (read: older) rule of thumb was to have a swap space at least the size of physical RAM, if not twice as large.\nHowever, you also need to consider growth. If you expect to increase your <glossary>RAM<\/glossary>\nin the future, you should consider that when you set how much space you are going to use for swap. <glossary>RAM<\/glossary>\njust slides into your system; increasing swap may require reinstalling the\n<glossary>operating system<\/glossary>, particularly if you have an older\nversion of Linux. (perhaps a very old version)\n<\/p>\n<p>\nSo, how much do you assign to swap? Good question. In general, I still\nsupport the suggestion is twice as much as <glossary>RAM<\/glossary>.\nThis is the &#8220;good reason&#8221; I mentioned for having more swap than physical\nRAM. Creating a large swap space is easier to do it now and\nwaste the space than to reinstall later. Another good reason is when you have\nmore than one user running graphical applications. In this case, then even\nsetting swap to two times <glossary>RAM<\/glossary>\nis reasonable. If all the space is taken up on the\nprimary hard disk, you can add a hard disk and use the swap command to add\nadditional swap space.\n<\/p>\n<concept id=\"\" description=\"The key factor in decided how much swap space to install is how many applications you will run and what kind of applications.\" \/>\n<p>\nThe key factor is how many applications you will run and what kind of\napplications. You need to consider the behavior of the application. If you\nare doing work with some kind of graphics (i.e. graphic design, ray tracing, and so\nforth), then you might need to conside more swap. If you have a lot of users on\nyour system, you might want more swap, as well.\n<\/p>\n<question id=\"\" type=\"F\" text=\"With SCSI hard disks, accessing swap is faster than accessing RAM.\" \/>\n<concept id=\"\" description=\"Accessing swap is slower than accessing RAM.\" \/>\n<p>\nKeep also in mind that accessing swap is slower than accessing RAM. You\nmight want to consider adding more RAM. I have a co-worker who has 3 GB of\nRAM in his system as he does a lot of graphical work and it is an extreme\nburden on his system to be constantly swapping in and out. He never gets\nclose to using all of his RAM, so he does not need a large swap space.\n<p>\nVersions of the Linux kernel prior to 2.4.10 &#8220;liked&#8221; to have at least\ntwice as much swap as RAM. However, with the newer kernels, this is no\nlonger true.\n<p>\n<p>\nOne important thing to consider is what happens when the system runs out of\nmemory. Being out of memory (OOM) is a dangerous thing and can cause the\nwhole system to come to a stand-still or even crash. Within the Linux\nmemory management functions is something called an &#8220;OOM killer&#8221; which will\nstop (or kill) processes when the system runs out of memory. The problem is\nthat currently (August 2006), processes are killed &#8220;blindly&#8221;. That is, without\nregard to their importance. In general, the system calculates\nhow &#8220;bad&#8221; the process is longer a process runs and the more memory it\nuses, the greater the chance is that it will be killed. (For details\nlook at the file <file type=\"\">\/usr\/src\/linux\/mm\/oom_kill.c<\/file>).\n<\/p>\n<p>\nThe biggest problem is that it arbitrary. That is a program could be running\ncorrectly, not causing any problems, but it just runs a lot and uses a lot\nof RAM. Imagine if this was an database. The argument whether the database\nshould be killed or the whole system stops is philosophical, so we won&#8217;t go\ninto it here. With 40 GB being a <i>small<\/i> hard disk today, allocating 1 GB swap\n(i.e. 2.5{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}) for peice of mind is not a bad idea.\n<p>\nYou also need to keep in mind that <glossary>swapping<\/glossary> takes\nup system resources. The time to access the hard disk is hundreds of times\nslower than the time to access <glossary>RAM<\/glossary>.\nTherefore, if speed is an important\nconsideration, you should think about having enough <glossary>RAM<\/glossary>\nso you don&#8217;t swap. The maximum size of your swap space depends on your hardware\narchitecture and more recent kernels on the i386 can have swap partitions that are as\nlarge as 2Gb and you can have as many as 8 different swap partitions if you\nhave kernel older than 2.4.10. Later versions support up to 32 swap spaces.\n<\/p>\n<concept id=\"\" description=\"Linux allows you to create a swap file.\" \/>\n<question id=\"\" type=\"TF\" text=\"Linux allows you to create a swap file.\" \/>\n<question id=\"\" type=\"MC\" text=\"Which of the following is not true about swap spaces.\" \/>\n<p>\nNote that I said swap <i>spaces<\/i> and not just swap device or swap partition.\nLinux allows you to create a swap <i>file<\/i>. Like any other file, a swap\nfile exists on your filesystem and takes up space. The advantage is that you\ncan add a swap file at any time, provided you have the space on the hard disk.\nYou don&#8217;t need to to repartition your hard disk or even reboot.\n<\/p>\n<p>\nThere are two different swap versions (or formats). Kernels prior 2.4 supports\nonly version 0 swap spaces. Versions later than Linux 2.1.117 support\nversion 0 and version swap. However, Linux 2.6 only supports version 1.\nTherefore you need to be careful when upgrading. The <command>mkswap<\/command> command\ncan format in either format. See the <command>mkswap<\/command> for more\ndetails.\n<\/p>\n<p>\nAnother change with the 2.4.10 kernel is that the swap spaces can be up to\n64 Gb in size. Note, however, that with some Linux distributions, the <command>mkswap<\/command> command can only create swap devices that are 2GB\nor smaller.\n<\/p>\n<p class=\"subtitle\">Managing Swap<\/p>\n<p>\nIn many cases, once the system is installed, you never have to think about\nswap again. However, when you start using your system more actively, add new\nsoftware, and so on, you will probably find that you should at least\ntake a look at your current swap usage.\n<\/p>\n<concept id=\"\" description=\"The top and free commands can be used to display swap usage.\" \/>\n<question id=\"\" type=\"MC\" text=\"Which of the following commands is not used to display memory-related information?\" \/>\n<p>\nLinux provides a number of tools to monitor swap. The easiest is the\n<command>free<\/command> command, which gives you a quick overview of the\nmemory usage. You can also use the <command>top<\/command> which can provide\nan self-updating view of your system, including memory usage by process, users\non the system, and so forth. Also the <file type=\"\">\/proc\/swaps<\/file> and\n<file type=\"\">\/proc\/meminfo<\/file> files\ncontain information about memory usage on your system.\n<p>\nLinux also provides tools to manage your swap space. You can add and remove\nspaces as you need to, as well as turn them on and off, even while they are\nbeing used.\n<\/p>\n<concept id=\"\" description=\"A file which will be used for swap space can be created using the dd command.\" \/>\n<p>\nTo create a file to be used as swap, you need to first create the file. This is\nmost easily done with the <command>dd<\/command> command. To create a 65 Mb file,\nyou might have this command (from the the mkswap man-page):\n<\/p>\n<p>\n<commandexample command=\"dd\">\ndd if=\/dev\/zero of=\/data\/swapfile.1 bs=1024 count=65536\n<\/commandexample>\n<p>\nwhich displays:\n<p>\n<screenoutput>\n65536+0 records in\n65536+0 records out\n<\/screenoutput>\n<concept id=\"\" description=\"The mkswap command prepares the swap space for use.\" \/>\n<p>\nNext you have to prepare the file for usage as swap space using\nthe <command>mkswap<\/command>. The simplest form would be:\n<p>\n<commandexample>\nmkswap device size\n<\/commandexample>\n<p>\nWhere &#8220;device&#8221; is either the name of a device node for a hard disk partition\nor the name of a file you want to use. The &#8220;size&#8221; option is only required\nwhen you create a swap file. However, it is actually superfluous and still\nmaintained for backwards compatibility. The command you issue migt look\nlike this:\n<p>\n<commandexample command=\"mkswap\">\nmkswap \/data\/swapfile.1\n<\/commandexample>\n<p>\nWhich displays:\n<\/p>\n<screenoutput>\nSetting up swapspace version 1, size = 67104 kB\n<\/screenoutput>\n<p>\nWhat that this does is format the swap space by adding the swap header\ninformation.\nNote this said that it created a version 1 swap space. We could have used\nthe -v0 option to create a version 0 swap space if we needed to.\n<\/p>\n<concept id=\"\" description=\"A swap file must be created to its fulle size before it is activated.\" \/>\n<p>\nOne key thing to keep in mind is that the kernel needs to have the swap file\ncreated to it&#8217;s full size before it is used. That is, it cannot be a sparse\nfile (one that only uses part of the allocated space).\n<\/p>\n<p>\nAt this point we are ready to activate the swap space. If you are adding the\nswap space permanently, then you will need to include it in your <file type=\"\">\/etc\/fstab<\/file>\nfile. My default (initial) swap space looks like this:\n<\/p>\n<filecontents>\n\/dev\/hda5    swap    swap    pri=42   0 0\n<\/filecontents>\n<p>\nThis basically says that the device <device>\/dev\/hda5<\/device> is to be mounted onto the special\nmount point swap, is of type swap, has a priority of 42 and that the filesystem\nshould not be dumped if the system crashes, nor should the filesystem be\nchecked on system boot. (The last two 0&#8217;s)\n<\/p>\n<p>\nTo automatically use the swap file we just created, we might add an\nentry that looks like this:\n<\/p>\n<filecontents>\n\/data\/swapfile.1 none    swap    pri=5,defaults 0 0\n<\/filecontents>\n<p>\nWhen your system boots, all swap devices will be added which are listed in\nthe <file type=\"\">\/etc\/fstab<\/file>, unless they have the &#8220;noauto&#8221; option (just like any normal\nfilesystem). If I wanted to immediate add the swap space without having to\nreboot, I can run swapon -a, which will activate all swap spaces in the\n<file type=\"\">\/etc\/fstab<\/file> file (again, unless they have the &#8220;noauto&#8221; option).\nIf the swap space is already in use, the system will silently ignore it.\n<\/p>\n<concept id=\"\" description=\"The priority of the swap space determines the order in\nwhich the swap space is used\" \/>\n<p>\nAs you might guess, the priority of the swap space determines the order in\nwhich the swap space is used. The higher the priority the sooner it will be\nused. In this example, the primary swap space in its own parition has a\npriority of 42 and will be used before the swap file with a priority of 5.\n<\/p>\n<p>\nWe can also add swap space dynamically using the <command>swapon<\/command>\ncommand. After creating the swap space, you might activate it with this\ncommand:\n<\/p>\n<p>\n<commandexample command=\"swapon\">\nswapon \/data_c2\/swapfile.1\n<\/commandexample>\n<p>\nTo show what is currently beeing used as swap space we issue the command\n<\/p>\n<commandexample command=\"swapon\">\nswapon -s\n<\/commandexample>\n<p>\nThis might show us:\n<\/p>\n<p>\n<screenoutput>\nFilename                        Type            Size    Used    Priority\n\/dev\/hda5                       partition       409616  200560  42\n\/data_c2\/swapfile.1             file            65528   0       -1\n<\/screenoutput>\n<p>\nJust as you can enable swap from the command line, you can also turn it off.\nThis is done with the swapoff command and it might look like this:\n<\/p>\n<p>\n<commandexample command=\"swapoff\">\nswapoff \/data_c2\/swapfile.1\n<\/commandexample>\n<p>\nFor more details see the <command>swapoff<\/command> man-page.\n<\/p>\n<p>\nIf performance is really an issue thare are number of different things\nyou can do. If you have multiple hard disk on different controllers, you can\nput swap spaces on each device and give them the same priority. This works\nlike traditional disk &#8220;striping&#8221;, whereby the kernel uses both swap spaces in\nparallel.\n<\/p>\n<concept id=\"\" description=\"You can increase performance by seperating the swap devices from data devices.\" \/>\n<p>\nAnother way of increasing performance is to seperate the swap devices from\ndata devices. If you have multiple hard disk controllers (or maybe a single\n<glossary>SCSI<\/glossary> <glossary>host adapter<\/glossary> with multiple\nchannels, you can put the data on one channel and the hard disk with\nthe swap service on a different channel. Putting the data on the SCSI devices\nand the swap device on an IDE controller may also be possible. In essence,\nyou are trying to keep swap on the less used devices, so that the heavily\nused devices don&#8217;t get slowed down by swap. Also putting difference swap devices on\ndifferent IDE controllers will increase throughput.\n<\/p>\n<p>\nLet&#8217;s assume that we have four IDE hard disks. We might have something in our\n\/<file type=\"fstab\">etc\/fstab<\/file> that looks like this:\n<\/p>\n<filecontents type=\"fstab\">\n\/dev\/hda2 none  swap  sw,pri=10 0 0\n\/dev\/hdb2 none  swap  sw,pri=10 0 0\n\/dev\/hdc2 none  swap  sw,pri=10 0 0\n\/dev\/hdd2 none  swap  sw,pri=1  0 0\n<\/filecontents>\n<concept id=\"\" description=\"You can specify the priority of swap devices and thus determine the order in which each device is used.\" \/>\n<p>\nThe priority for all of the first three swap partitions is 10. This means they all have the same priority and since Linux uses swap devices with a higher priority <em>first<\/em>,\nthese three will be used before the last one. Note that in this case, the system will <strong>not<\/strong> wait until the first one is full before using the second one. Because\nthe swap devices all have the same priority, Linux will <em>parallelize<\/em> the access. That means the the start of <em>each<\/em> swap partition will be used first, which has\nfaster access than areas further out.\nNote that the devices hda2 and hdb2 are on seperate drives, but they use the same controller. This is not as efficient as using hda2 and hdc2, which are on seperate controllers. For more details take look at the <command>mkswap<\/command> command.\n<\/p>\n<p>\nWith the Linux 2.6 and later kernel, swap files are basically as efficient\nas swap devices. This is useful as swap files are easier to administer than\nswap devices. You can add, remove and resize then as you need to. If you have\ntwo hard disks, then you can create swap files on each devices (with the same\npriority) and improve swap performance.\nHowever, always keep in mind that swap space is just a safety mechanism.\nIf you really\nneed to swap often then the best thing is to buy more RAM.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swap Space Swap Basics The traditional (read: older) rule of thumb was to have a swap space at least the size of physical RAM, if not twice as large. However, you also need to consider growth. If you expect to &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=369\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-369","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=369"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/369\/revisions"}],"predecessor-version":[{"id":715,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/369\/revisions\/715"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}