Ubuntu 22.04 Swap Space

From CompleteNoobs
Jump to navigation Jump to search
Please Select a Licence from the LICENCE_HEADERS page
And place at top of your page
If no Licence is Selected/Appended, Default will be CC0

Default Licence IF there is no Licence placed below this notice! When you edit this page, you agree to release your contribution under the CC0 Licence

LICENCE: More information about the cc0 licence can be found here:
https://creativecommons.org/share-your-work/public-domain/cc0

The person who associated a work with this deed has dedicated the work to the public domain by waiving all of his or her rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law.

You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Licence:

Statement of Purpose

The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work").

Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others.

For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights.

1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following:

   the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work;
   moral rights retained by the original author(s) and/or performer(s);
   publicity and privacy rights pertaining to a person's image or likeness depicted in a Work;
   rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below;
   rights protecting the extraction, dissemination, use and reuse of data in a Work;
   database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and
   other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof.

2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose.

3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose.

4. Limitations and Disclaimers.

   No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document.
   Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law.
   Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work.
   Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work.

Swap Space

Swap space is an area on your hard drive that the Ubuntu operating system uses as a temporary storage location when your system runs out of physical memory (RAM). When your system's RAM is full, the operating system moves less frequently used data from RAM to the swap space, freeing up RAM for more critical processes. This process is called "swapping" or "paging."

Ubuntu swap space is a dedicated partition or file on your hard drive that functions as virtual memory for your Linux operating system. When your system runs out of physical RAM, it uses swap space to temporarily store data that is not actively being used. This enables the operating system to continue running smoothly and efficiently even when memory is scarce.

Here are some reasons why you might need swap space:

  • Memory management: Swap space acts as an overflow buffer for your system's memory. When your RAM is full, the least-used data is moved to swap space, freeing up RAM for more critical tasks. This helps prevent system crashes and slowdowns due to insufficient memory.
  • Hibernation: If you use hibernation on your Ubuntu system, swap space is essential. During hibernation, the contents of your RAM are saved to swap space, allowing your system to power down entirely. When you power your computer back on, the data stored in swap space is loaded back into RAM, and you can continue where you left off.

Determining whether you need swap space depends on your system's resources and usage patterns. Here are some general guidelines:

  • If you have a system with a large amount of RAM (16 GB or more), you might not need swap space unless you're running memory-intensive applications or using hibernation.
  • For systems with less RAM, it's generally a good idea to have some swap space available. A common rule of thumb is to allocate swap space equal to the amount of RAM in your system, but this may vary based on your specific needs.
  • If your system frequently runs out of RAM, increasing swap space can help alleviate the issue. However, it's important to note that swap space is slower than RAM because it uses your hard drive. If performance is a concern, consider upgrading your RAM.

To summarize, swap space in Ubuntu serves as an extension of your system's RAM, providing extra memory when needed and enabling hibernation. Whether or not you need swap space depends on your system's resources and how you use your computer.

Notes on SSD's

For systems with SSDs, using swap space can increase wear on the drive due to frequent read and write operations. Unlike traditional hard drives, SSDs have a limited number of program/erase (P/E) cycles, which means that each memory cell can only be written and erased a certain number of times before it starts to degrade.

When swap space is used on an SSD, it may lead to more write operations, potentially causing the drive to wear out faster. However, modern SSDs are designed with wear leveling algorithms that help distribute these write operations evenly across the drive, prolonging its lifespan. Additionally, SSD technology has improved significantly in recent years, and the P/E cycle limits of current SSDs are much higher than those of older models, making them more durable.

To mitigate the potential wear and tear on your SSD from swap space usage, you can consider the following options:

  • Reduce swap usage - Explained on page
  • Allocate more RAM: If you have sufficient RAM, your system will rely less on swap space, reducing the number of write operations on your SSD. Consider upgrading your RAM if you frequently run memory-intensive applications.
  • Use a separate HDD for swap: If your system has both an SSD and a traditional hard drive (HDD), you can choose to allocate swap space on the HDD instead. This will spare your SSD from the additional wear caused by swap usage.

Check your Swap Space

First check if you already have a swap space
free -m

cat /proc/swaps

swapon -s -v

Creating a swap space

preallocate a 2 gigabyte space to be used for swap.
Note: you can name swapfile to anything you want.

fallocate -l 2G /swapfile

Initialize the /swapfile file with zeros - see "Working out the count size" to see how to work out the count size.

dd if=/dev/zero of=/swapfile bs=1024 count=2097152

chmod 600 /swapfile

mkswap /swapfile

swapon /swapfile Or swapon -a Or mount -a

append to /etc/fstab so its mounted after reboot.
/swapfile swap swap defaults 0 0

and thats it, you can check swap with.

cat /proc/swaps

swapon -s -v

free -m


Working out the count size

Working out count size to scale the swapsize.

1 kilobyte = 1024 bytes

1 megabyte = 1024 kilobyte

1 gigabyte = 1024 megabytes

bs=1024 = 1 megabyte

1 gigabyte = 1024 megabyte

to get count of 1 gigabyte 1024 * 1024

2 gigabyte (1024 * 2048 ) or 1048576 * 2 = 2,097,152

Reducing Swap Usage

Reduce swap usage: You can configure your system to use swap space less aggressively by adjusting the "swappiness" parameter. A lower swappiness value (e.g., 10) will make the system use swap space less often, prioritizing the use of RAM instead. This can be done by editing the /etc/sysctl.conf file and adding or modifying the following line:

vm.swappiness=10

Then, save the file and reboot your system for the changes to take effect.

The vm.swappiness parameter is a Linux kernel setting that controls how aggressively the kernel uses swap space. It is a value between 0 and 100, where a higher value makes the system more likely to use swap space, while a lower value makes the system prefer keeping data in RAM. The default value for most Linux distributions, including Ubuntu, is usually set to 60.

Here's a more detailed explanation of how different vm.swappiness values affect system behavior:

  • A value of 0: The system will only use swap space when it has completely exhausted the available RAM, avoiding swap usage as much as possible. This is not recommended for most systems, as it can lead to poor performance and possible crashes when the system runs out of memory.
  • A value between 1 and 100: The higher the value, the more aggressively the system will use swap space. A higher swappiness value can help free up more RAM for caching, which might be beneficial on systems with limited memory. However, it may also increase the number of disk read and write operations, potentially affecting performance, especially on slower storage devices like HDDs.
  • A value of 100: The system will aggressively use swap space even when there's plenty of free RAM available. This setting is generally not recommended, as it can lead to excessive disk I/O and degrade system performance.

Adjusting the vm.swappiness value allows you to fine-tune the balance between using RAM and swap space according to your system's resources and performance requirements. For example, if you have an SSD and want to reduce wear and tear on the drive, you can set a lower swappiness value (e.g., 10) to make the system use swap space less frequently. On the other hand, if you have an HDD and limited RAM, you might opt for a higher swappiness value to free up more RAM for caching and avoid running out of memory.

To change the vm.swappiness value, edit the /etc/sysctl.conf file and add or modify the following line:


vm.swappiness=<your_desired_value>

Replace <your_desired_value> with a number between 0 and 100 that suits your system's needs. After making the change, save the file and reboot your system for the changes to take effect.