- Linux Find Out What Process Are Using Swap Space
- Finding out process ID and swap usage
- Listing all process swap space usage
- Say hello to smem
- Installation
- How do I use smem command?
- Options
- A note about top command
- Linux swap: what it is and how to use it
- Video
- What is Linux Swap?
- Do you need Linux Swap?
- Linux Swap Partition
- Linux Swap File
- How to remove a Linux Swap File
- How to adjust the Swappiness value
- Conclusion
- Ubuntu Documentation
- Introduction
- What is swap?
- Why do I need swap?
- How much swap do I need?
- Example Scenarios
- How do I add more swap?
- 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 swap partition work for hibernate (optional)
- How do I add a swap file?
- Create the Swap File:
- Enable use of Swap File
- Enable Swap File at Bootup
- Example of making a swap file
- Disable and Remove a Swap File
- What is swappiness and how do I change it?
- What is the priority of swap containers?
- Should I reinstall with more swap?
- Why is my swap not being used?
- Swap may not be needed
- Is there a swap partition at all?
- Enabling a swap partition
- Empty Swap
Linux Find Out What Process Are Using Swap Space
T he top and free command display the total amount of free and used physical and swap memory in the server. How do I determine which process is using swap space under Linux operating systems? How do I find out swap space usage of a particular process such as memcached?
Tutorial details | |
---|---|
Difficulty level | Intermediate |
Root privileges | Yes |
Requirements | Linux with smem and /proc |
Est. reading time | 6 minutes |
You can use the any one of the following techniques but keep in mind that because of shared pages, there is no reliable way to get this information [ 1]
- /proc/meminfo – This file reports statistics about memory usage on the system. It is used by free to report the amount of free and used memory (both physical and swap) on the system as well as the shared memory and buffers used by the kernel. You can also use free, vmstat and other tools to find out the same information.
- /proc/$
/smaps , /proc/$ /status , and /proc/$ /stat : Use these files to find information about memory, pages and swap used by each process using its PID. - smem – This command (python script) reports memory usage with shared memory divided proportionally.
Finding out process ID and swap usage
- 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 ➔
To see swap space used by memcached (PID # 48440), enter (number #2):
# grep —color VmSwap /proc/48440/status
Sample outputs (number #4):
Or the following awk command (number #3):
# awk ‘/VmSwap/
Sample outputs (number #4):
Fig.01: Finding out memcached process swap usage on Linux
Listing all process swap space usage
Type the following bash for loop command to see swap space usage per process:
Type the following command to sort out output:
Say hello to smem
The smem command reports physical memory usage, taking shared memory pages into account. Unshared memory is reported as the USS (Unique Set Size). Shared memory is divided evenly among the processes sharing that memory. The unshared memory (USS) plus a process’s proportion of shared memory is reported as the PSS (Proportional Set Size). The USS and PSS only include physical memory usage. They do not include memory that has been swapped out to disk. Memory can be reported by process, by user, by mapping, or system-wide. Both text mode and graphical output are available.
Installation
To install smem [ 2] type the following command under Debian / Ubuntu Linux:
$ sudo apt-get install smem
RHEL / CentOS Linux user type the following command:
$ wget https://www.selenic.com/smem/download/smem-1.4.tar.gz
$ tar xvf smem-1.4.tar.gz
# cp /tmp/smem-1.2/smem /usr/local/bin/
# chmod +x /usr/local/bin/smem
How do I use smem command?
To see basic process information, enter:
# smem
Sample outputs:
To see library-oriented view, enter:
# smem -m
To see user-oriented view, enter:
# smem -u
Sample outputs:
To see systemwide memory usage summary pass the -w option:
# smem -w
Sample outputs:
To see system view
# smem -R 8G -K /path/to/vmlinux/on/disk -w
To see totals and percentages, enter:
# smem -t -p
Sample outputs:
Options
Type the following command to see all other supported options:
# smem —help
Sample outputs:
A note about top command
Type the top command as root:
# top
To sort process as per swap page usage (SWAP = VIRT – RES) type capital O (option) followed by p (small p ) and [Enter] key:
Fig.02 top command – sored process by swap usage (click to enlarge)
References:
It is not possible to get the exact size of used swap space of a process. The top command fakes this information by making SWAP = VIRT – RES, but that is not a good metric, because other stuff such as video memory counts on VIRT as well (for example: top says my X process is using 81M of swap, but it also reports my system as a whole is using only 2M of swap. Therefore, I will not add a similar Swap column to htop because I don’t know a reliable way to get this information (actually, I don’t think it’s possible to get an exact number, because of shared pages).
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Linux swap: what it is and how to use it
If you are a Linux user you have definitely heard about Linux Swap, but you may not know what it is exactly or have only superficial knowledge about it. Time to change that!
I mention Linux swap and swappiness in almost every article on things to do after installing Linux. You know that Linux swap is somehow related to RAM and swappiness can affect your system performance. It is time to explain what it is and how to use it in little more details.
Video
What is Linux Swap?
The Linux Kernel divides RAM into chunks of memories and the swapping process is when the Linux Kernel uses a hard disk space (swap space) to store information from RAM and thus releases some RAM space. That is why when you install a Linux distribution, the installation wizard usually asks you to assign some space for the system and another for the swap.
Using swap is a very useful way to extend the RAM because it provides the necessary additional memory when the RAM space has been exhausted and a process has to be continued. It is especially recommended when you have less than 1Gb of RAM. Although in the end, everything depends on you.
Do you need Linux Swap?
This is a question many novice users ask themselves when they begin to discover Linux. In fact, this will depend on the use and amount of RAM your computer has. Regarding the use, there are processes and applications that really use a lot of memory, for example, Google Chrome. However, most of the current equipment comes with at least 8Gb of RAM and that makes the swap process less necessary. Nevertheless, having a swap space is desirable even if you have a lot of RAM.
For example, usually, when your RAM gets full and the Linux kernel has no space to write into, your system will crash. On the other hand, if you have a swap space, it will be used by the Linux kernel and your system will keep working, though much slower. So, it is safer to have swap space.
Note: swap space has one disadvantage — it is much slower than RAM. So, adding a swap space will not make your computer faster, it will only help to overcome some limitations posed by RAM size.
Linux Swap Partition
I recommend that you create the swap partition during the installation of your Linux distribution. In general, these are the recommended sizes for the swap partition.
- If your computer has 1Gb of RAM or less, then the swap partition should be twice the size of the RAM.
- But, If you have between 2gb and 4gb of RAM, the size of the swap partition should be half the RAM.
- Finally, If you have more than 4gb of RAM, then it is enough to have 2Gb.
But everything depends on your use case.
You can check the type and size of your swap with this command:
Above, I have a swap partition of 2Gb.
Linux Swap File
Alternatively, you can create a Linux Swap File after the installation. The modern Linux Kernel allows Swapping to a swap file instead of a swap partition. A swap file has an advantage over a swap partition that you can change the size of your swap any time easily by changing a swap file size.
If you want to create a swap file, run this command first:
Note: this command is to create a 1Gb swap file. Replace 1G with the value you want.
Next, you have to set the correct permissions.
Then, format the file to swap.
Finally, enable the swap.
If you want the changes to be permanent, you need to edit the /etc/fstab file and add the following.
In the end, check the status of the swapfile:
If you see the size of a swap file in the total column, you have done everything correctly.
How to remove a Linux Swap File
In case you need to remove a Linux swap file for any reason, you need to follow these steps.
First, deactivate the swap.
If you created the entry in the /etc/fstab file, remove it. To remind you, it is the line: /swapfile swap swap defaults 0 0 entry.
Finally, delete the actual Linux Swap File.
How to adjust the Swappiness value
Swappiness is a property of the Linux Kernel to define how often the swap space will be used. As you know RAM is faster than a hard drive. So, every time you need to use swap, you will notice that some processes and applications will run slower. However, you can adjust the system to use much more RAM than swap. This can help improve overall system performance.
Normally, the default swappiness value is 60. The smaller this value, the more of your RAM will be used.
To verify the swappiness value, run this command:
You should see the value of 60.
If you want to modify the default value, you need to edit the file /etc/sysctl.conf .
And add the following (10 is the most commonly recommended value):
Save the file and close it with Ctrl+O and Ctrl+X shortcuts. In order to apply the changes, you need to reboot the system.
This way your Linux kernel will use more RAM and less swap, but it still will swap when your RAM memory gets critically full. Usually, this setting is recommended when you have more than 4Gb of RAM.
Conclusion
In conclusion, it is safer to have some swap space on your computer. You can use either a swap partition or a swap file. The latter becomes more and more common.
Linux swap is a technical concept but knowing how it works can improve the performance of the system. Just play with the swappiness value.
Do you have anything to add about Linux swap? Have you changed the default value of swappiness? Let me know in the comments.
Источник
Ubuntu Documentation
Introduction
This FAQ is aimed at Linux novices.
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
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.
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
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
Источник