Linux swap file size

Содержание
  1. How to Increase Swap Size on Ubuntu Linux
  2. Increase swap size on Ubuntu
  3. How To Check Swap Usage Size and Utilization in Linux
  4. Check swap usage size and utilization in Linux
  5. How to Check Swap Space in Linux using /proc/swaps file
  6. Look for swap space in Linux using swapon command
  7. Use free command to monitor swap space usage
  8. See swap size in Linux using vmstat command
  9. top/atop/htop/glances command
  10. Linux Find Out What Process Are Using Swap Space
  11. Linux GUI tool to monitor swap space size and usage
  12. Conclusion
  13. Linux swap file size
  14. Contents
  15. Swap space
  16. Swap partition
  17. Activation by systemd
  18. Disabling swap
  19. Swap file
  20. Manually
  21. Swap file creation
  22. Remove swap file
  23. Automated
  24. zram-generator
  25. systemd-swap
  26. Swap encryption
  27. Performance
  28. Swappiness
  29. VFS cache pressure
  30. Priority
  31. Using zswap or zram
  32. Striping
  33. Ubuntu Documentation
  34. Introduction
  35. What is swap?
  36. Why do I need swap?
  37. How much swap do I need?
  38. Example Scenarios
  39. How do I add more swap?
  40. How do I add or modify a swap partition?
  41. Process to Increase Size of Swap Partition and use it for Hibernation
  42. Creating the swap partition
  43. Activating the swap partition
  44. Making the swap partition work for hibernate (optional)
  45. How do I add a swap file?
  46. Create the Swap File:
  47. Enable use of Swap File
  48. Enable Swap File at Bootup
  49. Example of making a swap file
  50. Disable and Remove a Swap File
  51. What is swappiness and how do I change it?
  52. What is the priority of swap containers?
  53. Should I reinstall with more swap?
  54. Why is my swap not being used?
  55. Swap may not be needed
  56. Is there a swap partition at all?
  57. Enabling a swap partition
  58. Empty Swap

How to Increase Swap Size on Ubuntu Linux

The recent releases of Ubuntu use swap file instead of the traditional swap partition. The swap file is simply a file under the root which is used as swap to share the burden on the RAM.

The biggest advantage of using a swap file is that you can easily resize it. That’s not always the case when you use a dedicated swap partition.

Let’s see how to resize the swap space on Ubuntu.

Increase swap size on Ubuntu

If you are using swap partition and want to increase the swap size, you can create swap file. Your Linux system can use multiple swap spaces as needed. This way, you won’t have to touch the partition.

This tutorial assumes that you are using swap file on your system, not a swap partition.

Now, let’s see how to increase the swap file. First thing first, make sure that you have a swap file in your system.

It will show the current swap available. If you see the type file, it indicates that you are using a swap file.

Now before you resize the swap file, you should turn the swap off. You should also make sure that you have enough free RAM available to take the data from swap file. Otherwise, create a temporary swap file.

You can disable a given swap file using this command. The command doesn’t produce any output and it may take a few minutes to complete:

Now use the fallocate command in Linux to change the size of the swap file.

Make sure that you mark this file as swap file:

You should see an output like this where it warns you that old swap signature is being wiped out.

Once you do that, enable the swap file:

That’s it. You just increased the swap size in Ubuntu from 2 GB to 4 GB. You can check swap size using the free command or the swapon —show command.

You see how easy it is to resize swap size thanks to the swap files. You didn’t touch the partition, you didn’t reboot the system. Everything was done on the fly. How cool is that!

I hope you found this quick tutorial helpful in resizing the swap space on Ubuntu as well as other Linux distributions. If you have questions or suggestions, please leave a comment below.

Источник

How To Check Swap Usage Size and Utilization in Linux

H ow do I check swap (paging) usage under Linux operating systems using command bash/ksh line options? How do I check swap usage size on Linux operating system?

Swap space (also known as paging) is nothing but computer memory management involving swapping regions of memory to and from storage. You can see swap usage summary by device using any one of the following commands. You may have to login as root user to use the following commands.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements None
Est. reading time 5m

The maximum useful size of a swap area depends on the architecture and the kernel version. For Linux kernels after v2.3.3+ there is no such limitation on swap size.

Check swap usage size and utilization in Linux

The procedure to check swap space usage and size in Linux is as follows:

  1. Open a terminal application.
  2. To see swap size in Linux, type the command: swapon -s .
  3. You can also refer to the /proc/swaps file to see swap areas in use on Linux.
  4. Type free -m to see both your ram and your swap space usage in Linux.
  5. Finally, one can use the top or htop command to look for swap space Utilization on Linux too.

How to Check Swap Space in Linux using /proc/swaps file

Type the following cat command to see total and used swap size:
# cat /proc/swaps
Sample outputs:

Another option is to type the grep command as follows:
grep Swap /proc/meminfo

Look for swap space in Linux using swapon command

Type the following command to show swap usage summary by device
# swapon -s
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Use free command to monitor swap space usage

Use the free command as follows:
# free -g
# free -k
# free -m
Sample outputs:

See swap size in Linux using vmstat command

Type the following vmstat command:
# vmstat
# vmstat 1 5
Sample outputs:

Note down the following output from swap field:

  1. si: Amount of memory swapped in from disk (/s).
  2. so: Amount of memory swapped to disk (/s).

top/atop/htop/glances command

Type the following commands:
# atop
# htop
# top
# glances
Sample outputs from top command:

Sample outputs from htop command:

Fig.01: Linux: Swap Memory Usage Command

Linux Find Out What Process Are Using Swap Space

Try smem command:
smem
OR
top

Linux GUI tool to monitor swap space size and usage

Try Gnome or KDE system monitor tool. For example, the GNOME System Monitor shows you what programs are running and how much processor time, memory (including paging/swap space size), and disk space are being used.

Conclusion

This page showed you how to check for swap space size and utilization in Linux. If you see a large percentage of the swap space utilization, then it is time to add more physical RAM to the Linux system. Another option is to increase swap space by adding a swap file on Linux. Please see the following resources for more info:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Linux swap file size

This page provides an introduction to swap space and paging on GNU/Linux. It covers creation and activation of swap partitions and swap files.

Linux divides its physical RAM (random access memory) into chunks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Support for swap is provided by the Linux kernel and user-space utilities from the util-linux package.

Contents

Swap space

Swap space can take the form of a disk partition or a file. Users may create a swap space during installation or at any later time as desired. Swap space can be used for two purposes, to extend the virtual memory beyond the installed physical memory (RAM), and also for suspend-to-disk support.

If it is beneficial to extend the virtual memory with swap depends on the amount of installed physical memory. If the amount of physical memory is less than the amount of memory required to run all the desired programs, then it may be beneficial to enable swap. This avoids out of memory conditions, where the Linux kernel OOM killer mechanism will automatically attempt to free up memory by killing processes. To increase the amount of virtual memory to the required amount, add the necessary difference (or more) as swap space.

The biggest drawback of enabling swap is its lower performance, see section #Performance. Hence, enabling swap is a matter of personal preference: some prefer programs to be killed over enabling swap and others prefer enabling swap and slower system when the physical memory is exhausted.

To check swap status, use:

Or to show physical memory as well as swap usage:

Swap partition

The factual accuracy of this article or section is disputed.

A swap partition can be created with most GNU/Linux partitioning tools. Swap partitions are typically designated as type 82 . Even though it is possible to use any partition type as swap, it is recommended to use type 82 in most cases since systemd will automatically detect it and mount it (see below).

To set up a partition as Linux swap area, the mkswap(8) command is used. For example:

To enable the device for paging:

To enable this swap partition on boot, add an entry to /etc/fstab :

where the device_UUID is the UUID of the swap space.

See fstab for the file syntax.

Activation by systemd

systemd activates swap partitions based on two different mechanisms. Both are executables in /usr/lib/systemd/system-generators . The generators are run on start-up and create native systemd units for mounts. The first, systemd-fstab-generator , reads the fstab to generate units, including a unit for swap. The second, systemd-gpt-auto-generator inspects the root disk to generate units. It operates on GPT disks only, and can identify swap partitions by their type GUID, see systemd#GPT partition automounting for more information.

Disabling swap

To deactivate specific swap space:

Alternatively use the -a switch to deactivate all swap space.

Since swap is managed by systemd, it will be activated again on the next system startup. To disable the automatic activation of detected swap space permanently, run systemctl —type swap to find the responsible .swap unit and mask it.

Swap file

As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).

Manually

Swap file creation

Use dd to create a swap file the size of your choosing. For example, creating a 512 MiB swap file:

Set the right permissions (a world-readable swap file is a huge local vulnerability):

After creating the correctly sized file, format it to swap:

Activate the swap file:

Finally, edit the fstab configuration to add an entry for the swap file:

For additional information, see fstab#Usage.

Remove swap file

To remove a swap file, it must be turned off first and then can be removed:

Finally remove the relevant entry from /etc/fstab .

Automated

zram-generator

The aim of this tool is the creation of zram devices. It is written in Rust and resides in systemd’s GitHub. It can be installed with the zram-generator package. Configuration is straightforward and explained in the README.

systemd-swap

systemd-swap is a script for creating hybrid swap space from zram swaps, swap files and swap partitions. It is not affiliated with the systemd project.

Install the systemd-swap package. Uncomment and set swapfc_enabled=1 in the Swap File Chunked section of /etc/systemd/swap.conf . Start/enable the systemd-swap service.

Visit the authors GitHub page for more information and setting up the recommended configuration.

Swap encryption

Performance

Swap operations are usually significantly slower than directly accessing data in RAM. Disabling swap entirely to improve performance can sometimes lead to a degradation, since it decreases the memory available for VFS caches, causing more frequent and costly disk I/O.

Swap values can be adjusted to help performance:

Swappiness

The swappiness sysctl parameter represents the kernel’s preference (or avoidance) of swap space. Swappiness can have a value between 0 and 200 (max 100 if Linux /sys/fs/cgroup/memory/memory.swappiness or /proc/sys/vm/swappiness can be read in order to obtain the raw integer value.

To temporarily set the swappiness value:

To set the swappiness value permanently, create a sysctl.d(5) configuration file. For example:

To test and more on why this may work, take a look at this article.

VFS cache pressure

Another sysctl parameter that affects swap performance is vm.vfs_cache_pressure , which controls the tendency of the kernel to reclaim the memory which is used for caching of VFS caches, versus pagecache and swap. Increasing this value increases the rate at which VFS caches are reclaimed[1]. For more information, see the Linux kernel documentation.

Priority

If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. The system will use swap areas of higher priority before using swap areas of lower priority. For example, if you have a faster disk ( /dev/sda ) and a slower disk ( /dev/sdb ), assign a higher priority to the swap area located on the fastest device. Priorities can be assigned in fstab via the pri parameter:

Or via the —priority parameter of swapon:

If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.

Using zswap or zram

Zswap is a Linux kernel feature providing a compressed write-back cache for swapped pages. This increases the performance and decreases the IO-Operations. ZRAM creates a virtual compressed Swap-file in memory as alternative to a swapfile on disk.

Striping

There is no necessity to use RAID for swap performance reasons. The kernel itself can stripe swapping on several devices, if you just give them the same priority in the /etc/fstab file. Refer to The Software-RAID HOWTO for details.

Источник

Ubuntu Documentation

Introduction

This FAQ is aimed at Linux novices.

  • People always wonder how much swap they should create at install time, or after installing they may think, «have I made a large enough swap? Should I reinstall with a larger swap?»
  • This FAQ will tell you how much swap you need and how to add more swap after installation.
  • You will be given very simple answers (to prevent losing too much time reading this FAQ) and some explanations that may help you form your own opinion.
  • What is swap?

    Swap space is the area on a hard disk. It is a part of your machine’s Virtual Memory, which is a combination of accessible physical memory (RAM) and the swap space. Swap holds memory pages that are temporarily inactive. Swap space is used when your operating system decides that it needs physical memory for active processes and the amount of available (unused) physical memory is insufficient. When this happens, inactive pages from the physical memory are then moved into the swap space, freeing up that physical memory for other uses. Note that the access time for swap is slower, depending on the speed of the hard drive. Do not consider it to be a complete replacement for the physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap file(s).

    Why do I need swap?

    Memory consuming programs Sometimes, a large program (like LibreOffice, Neverwinter Nights, or a video editor) make the entire system need extra memory. A significant number of the pages used by these large programs during its startup may only be used for initialization and then never used again. The system can swap out those pages and free the memory for other programs or even for the disk cache. In these cases, swap will be used to help the system handle any extra load.

    Hibernation (suspend-to-disk) The hibernation feature (suspend-to-disk) writes out the contents of RAM to the swap partition before turning off the machine. Therefore, your swap partition should be at least as big as your RAM size. Although the latest versions of Ubuntu don’t support hibernation OOTB you may configure your system to allow Hibernation. In both alternatives (PM-UTILS or SYSTEMD) you may use a partition or a file.

    Unforeseeable Circumstances Unforeseeable events can and will happen (a program going crazy, some action needing much more space than you thought, or any other unpredictable combination of events). In these cases, swap can give you an extra delay to figure out what happened, or to finish what you are working on.

    Optimizing memory usage Since mechanical hard drives are considerably slower than RAM (SSD — Solid State Drive — storage is not as slow as physical drives, but still slower than RAM), when you need a file (be it a data file like a video, executables like Firefox, or libraries), the Linux kernel reads the file into RAM and keeps it there, so that the next time you need it, it is already in RAM and data access is much faster. The portions of RAM that accelerate disk read are called «cached memory.» You will notice that they make a huge difference in terms of responsiveness. The Linux kernel automatically moves RAM reserved by programs—but not really used—into swap, so that it can serve the better purpose of extending cached memory.

    Optimizing Swap performance Because swap space uses a disk device, this can cause performance issues in any system that uses swap space significantly because the system itself may also be using the same disk device at the same time that it is required for swap operations. One way to reduce this problem is to have swap space on a different physical drive so that the competition for that resource is either reduced or eliminated.

    How much swap do I need?

    For less then 1GB of physical memory (RAM), it’s highly recommended that the swap space should, as a base minimum, be equal to the amount of RAM. Also, it’s recommended that the swap space is maximum twice the amount of RAM depending upon the amount of hard disk space available for the system because of diminishing returns.

    For more modern systems (>1GB), your swap space should be at a minimum be equal to your physical memory (RAM) size «if you use hibernation», otherwise you need a minimum of round(sqrt(RAM)) and a maximum of twice the amount of RAM. The only downside to having more swap space than you will actually use, is the disk space you will be reserving for it.

    The «diminishing returns» means that if you need more swap space than twice your RAM size, you’d better add more RAM as Hard Disk Drive (HDD) access is about 10³ slower then RAM access, so something that would take 1 second, suddenly takes more then 15 minutes! And still more then a minute on a fast Solid State Drive (SSD).

    Example Scenarios

    (last 3 columns denote swap space)

    How do I add more swap?

    Swap is generally associated with a swap partition, perhaps because the user is prompted to create a swap partition at the time of installation. In fact, any file can be used as a swapping device, be it a partition or a conventional file. Swap can be added by increasing the size of the swap partition or by adding a swap file. Keep in mind that when creating a swap file that it may not necessarily be using contiguous disk blocks (as a swap partition will), and this could have a negative impact on performance as disk access times may be longer, and the more your system uses swap, the worse it will be. The Linux kernel also accesses swap disk block IO directly bypassing all caching, metadata and filesystem code, so a swap file should have no ill effect on the stability of your base filesystem. Since kernel 2.6.29 the swap system has automatically supported TRIM capable devices like SSDs.

    The advantages of a swap file are many, but it is problematic for using the default swsusp hibernation method for powerless sleep. The best solution for maintaining hibernate capability after adding RAM is to increase the size of the swap partition.

    How do I add or modify a swap partition?

    Process to Increase Size of Swap Partition and use it for Hibernation

    • Creating the swap partition
    • Activating the swap partition
    • Making the new swap partition work for hibernate (optional)

    Creating the swap partition

      Boot to Ubuntu install CD and choose the option to run Ubuntu now

    Go to system -> GParted Partition Editor

  • Delete the swap partition and, if there is nothing else in it, the extended partition that holds it. (If by some miracle you’re able to resize your swap partition from here, I imagine your life will be a lot easier than mine.)
  • Decrease the size of your primary partition by the amount you want your new swap to be (I made mine 2x RAM + 500MB just to be safe). The easiest way to do this is to fill in the amount of space you want swap to be in the «free space following» field
  • In the free space that has now been created, choose new, type linux-swap and you can name the partition «swap» if you like
  • Hit the *Apply* button (should be a check mark) to write the changes to disk
  • When done, reboot back into Ubuntu
  • Activating the swap partition

    (If your swap is on your primary hard drive, you don’t need to do anything here.) Now you need to find what partition your swap is on and what its UUID is. UUID?! you say? Well that’s the Universally Unique IDentifier for the partition so you can reference it even if it’s on a different mount point from boot-to-boot due to adding disks, etc.

    Pull up a terminal and run gksu gparted & and enter your root password. The & lets this process run while still giving you access to the command line.
    Right-click on your swap partition and choose *Information*. You should see the **Path** and **UUID** listed there. Keep this open for further reference.

    Run gksu gedit /etc/fstab & and look for the line that has *swap* in it. It should be the third column, separated by spaces or tabs. You can either use the path or the UUID to tell Linux where to find your swap partition. I recommend UUID because it’ll stay constant even if you move the partition around or the disk somehow becomes sdb instead of sda or something like that. Make the appropriate edits and save the file. Your line should look something like this if you used UUID (with your UUID instead, of course):

    UUID=41e86209-3802-424b-9a9d-d7683142dab7 none swap sw 0 0

    or this if you used path: /dev/sda2 none swap sw 0 0

    Save the file.

    Enable the new swap partition with this command.

    Confirm that the swap partition exists.

  • Reboot to make sure the new swap gets activated properly at startup
  • Making the swap partition work for hibernate (optional)

    ‘INFO: This will not work for 12.04, resume from hibernate work differently in 12.04.

    Pull up a Terminal again and run cat /proc/swaps and hopefully you see the path to your swap partition listed there. If not chances are something went wrong in the steps above. Here’s my output:

    gksu gedit /etc/default/grub & to pull up the boot loader configuration

    Look for the line GRUB_CMDLINE_LINUX=»» and make sure it looks like this (using your UUID of course) GRUB_CMDLINE_LINUX=»resume=UUID=41e86209-3802-424b-9a9d-d7683142dab7″ and save the file

    sudo update-grub and wait for it to finish

    gksu gedit /etc/initramfs-tools/conf.d/resume & and make sure its contents are resume=UUID=41e86209-3802-424b-9a9d-d7683142dab7 (with your UUID of course in place of mine). Save the file!

    sudo update-initramfs -u

  • Reboot!
  • Now you should be able to hibernate and resume!

    How do I add a swap file?

    Note: btrfs does not support swap files at the moment. See man swapon. and btrfs Faq

    Create the Swap File:

    We will create a 1 GiB file ( /mnt/1GiB.swap) to use as swap:

    fallocate size suffixes: g = Giga, m = Mega, etc. (See man fallocate).

    If fallocate fails or it not available, you can use dd:

    We need to set the swap file permissions to 600 to prevent other users from being able to read potentially sensitive information from the swap file.

    Format the file as swap:

    Enable use of Swap File

    The additional swap is now available and verified with: cat /proc/swaps

    Enable Swap File at Bootup

    Add the swap file details to /etc/fstab so it will be available at bootup:

    Example of making a swap file

    This is an example of making and using a swap file on a computer with no swap partition.

    Disable and Remove a Swap File

    Disable the swap file from the running system and the delete it:

    Remove the swap file details from fstab:

    Removing the swap file line

    What is swappiness and how do I change it?

    The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

    • swappiness can have a value of between 0 and 100
    • swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible
    • swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache

    The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation. A value of swappiness=10 is recommended, but feel free to experiment. Note: Ubuntu server installations have different performance requirements to desktop systems, and the default value of 60 is likely more suitable.

    To check the swappiness value

    To change the swappiness value A temporary change (lost on reboot) with a swappiness value of 10 can be made with

    To make a change permanent, edit the configuration file with your favorite editor:

    Search for vm.swappiness and change its value as desired. If vm.swappiness does not exist, add it to the end of the file like so:

    Save the file and reboot.

    What is the priority of swap containers?

    The Linux kernel assigns priorities to all swap containers. To see the priorities that the Linux Kernel assigns to all the swap containers use this command.

    Priorities can be changed by using the swapon command or defined in /etc/fstab. Consult the manual page of swapon for more info

    Should I reinstall with more swap?

    Definitely not. With the 2.6 kernel, «a swap file is just as fast as a swap partition.» (Wikipedia:Paging, LKML).

    Why is my swap not being used?

    My swap is not being used! When I issue the free command, it shows something like this:

    Note: This regards mainly swap on hard disk partitions, but it could help anyway. In these examples /dev/hda8 is considered as swap.

    Swap may not be needed

    Start many memory consuming applications (e.g. Gimp, web browsers, LibreOffice etc) and then issue the free command again. Is swap being used now?

    Ubuntu Desktop uses Swap to Hibernate (PC off, no power needed, program states saved). If Hibernation is important to you, have more swap space than ram + swap overflow.

    Is there a swap partition at all?

    Use this command to see all partitions

    You should be able to see something like this in the output

    If not, you either need to create a swapfile or create a swap partition. To create a swap partition you can

      boot from your Ubuntu install CD, create a swap partition out of the free space on your hard disk and then interrupt your installation.

    Enabling a swap partition

    In case you do have a swap partition, there are several ways of enabling it.

    • Use the following command
    • Ensure that there is a line link below. This enables swap on boot.
    • Then disable all swap, recreate it, then re-enable it with the following commands.

    Empty Swap

    Even if you have lots of RAM and even if you have a low swappiness value, it is possible that your computer swaps. This can hurt the multitasking performance of your desktop system.

    You can use the following script to get the swap manually back into RAM:

    • Place the script e.g. /usr/local/sbin:
    • Copy-paste the script into the file:

    NOTE: The first «#!» is MoinMoin synxtax for comments, so we must double the line http://moinmo.in/HelpOnMoinWikiSyntax#Comments

    • Save and close gedit
    • Make the script executable:
    • Execute:

    SwapFaq (последним исправлял пользователь zubzou 2019-12-19 12:51:11)

    The material on this wiki is available under a free license, see Copyright / License for details
    You can contribute to this wiki, see Wiki Guide for details

    Источник

    Читайте также:  После сброса windows 10 до заводских настроек требует пароль
    Оцените статью