How to clear swap memory in Linux (2024)

Posted: February 25, 2020 | | by Tyler Carrigan (Editorial Team, Red Hat)

Swap memory is usually a "set it and forget it" type of affair. Most enterprise environments have swap built into the systems, and these memory caches are not manipulated unless there is an apparent lack of memory available or if a server crashes due to the OOM killer (out of memory) error. However, there is a niche situation that can cause an administrator to need to clear the system swap manually. If that is the situation that you find yourself in, you’ve come to the right place. This article is a discussion about this situation and the solution required.

Feeling swappy?

Occasionally, a system uses a high percentage of swap memory even when there is RAM available for use. The culprit here is the ‘swappiness’ of the system. Yep, you read that right...swappiness. So now that you know the lingo, you're ready to explorewhat it means. Swappiness refers to the kernel parameter responsible for how much and how often that the system moves data from RAM to swap memory.

The default value for swappiness is 60; however, you can manually set it anywhere between 0-100. Smallvalues cause littleswapping to occur, whereas high values can cause very aggressive swapping. A value of zero causes no swapping at all to occur, so if you want to minimize swapping to its lowest possible value without turning it off, you should set it to at least one.

[Free download: Advanced Linux commands cheat sheet.]

If you wanted to change up the swappiness of your system, the procedure is very straight-forward. You can check your current swappiness setting by running the following command:

$ cat /proc/sys/vm/swappiness

It should look something like this:

Image

How to clear swap memory in Linux (1)

Now, you can see that my system has a swappiness value of 30. To alter the value, you want to use the following:

$ sudo sysctl vm.swappiness=x(where x is the swap value you wish to set)

Image

How to clear swap memory in Linux (2)

To verify the value that you set, simply cat the swappiness file that youlooked at earlier to find out the original value. Easy day.Now that youunderstand the underlying parameters that control the swap behavior on our system, you're ready to learn how to clear that memory, should the situation arise. For the first time in your terminal life, things are going to be easy here.

[ For more information on swap space, seeManaging swap in the modern Linux system. ]

To clear the swap memory on your system, you simply need to cycle off the swap. This moves all data from swap memory back into RAM. It also means that you need to be sure you have the RAM to support this operation. An easy way to do this is to run 'free -m' to see what is being used in swap and in RAM. Once you power it off, you can wait an arbitrary amount of time (30 sec or so) to give the operation time to complete, then power the swap back on. This clears the swap memory cache and re-enables it. Here are all of the commands you'll need!

Check space: # free -m Disable swap: # swapoff -a Wait approx 30 sec (use free -m to see the amount of swap used/available decrease over time)Enable swap: # swapon -a 

Hopefully, this quick tip helps you clear your system swap memory if you ever find yourself in need of just such a fix.

[ Want to try out Red Hat Enterprise Linux? Download it now for free. ]

How to clear swap memory in Linux (2024)

FAQs

How to clear swap memory in Linux? ›

An easy way to do this is to run 'free -m' to see what is being used in swap and in RAM. Once you power it off, you can wait an arbitrary amount of time (30 sec or so) to give the operation time to complete, then power the swap back on. This clears the swap memory cache and re-enables it.

How do I clear memory space in Linux? ›

Every Linux System has three options to clear cache without interrupting any processes or services.
  1. Clear PageCache only.​​ #sync; echo 1 > /proc/sys/vm/drop_caches​
  2. Clear dentries and inodes. #sync; echo 2 > /proc/sys/vm/drop_caches.
  3. Clear PageCache, dentries and inodes. #sync; echo 3 > /proc/sys/vm/drop_caches.

How to remove swap space in Linux? ›

Removing a Swap File From Use
  1. Become superuser.
  2. Remove the swap space. # /usr/sbin/swap -d /path/filename. ...
  3. Edit the /etc/vfstab file and delete the entry for the swap file.
  4. Recover the disk space so that you can use it for something else. # rm /path/filename. ...
  5. Verify that the swap file is no longer available. # swap -l.

What happens when swap memory is full in Linux? ›

If the swap space is full, the system starts swapping out active memory, leading to performance degradation and even system crashes. Information loss. If the system loses power during intensive swapping, the swap file is not flushed to disk, resulting in data loss.

How do I manage swap memory in Linux? ›

IV. How to create and enable SWAP on Linux
  1. Step 1: SSH into your Linux system. ...
  2. Step 2: Check Swap. ...
  3. Step 3: Check the free disk space. ...
  4. Step 4: Create the Swap file. ...
  5. Step 5: Create Swap Partition and enable Swap on Linux.
Dec 20, 2020

How to clear swap memory? ›

To clear the swap memory on your system, you simply need to cycle off the swap. This moves all data from swap memory back into RAM. It also means that you need to be sure you have the RAM to support this operation. An easy way to do this is to run 'free -m' to see what is being used in swap and in RAM.

How do I clear unnecessary storage in Linux? ›

  1. Checking Disk Usage with df and du. Before starting the cleanup, it's essential to understand how much space is being used and where. ...
  2. Removing Unnecessary Files with rm. ...
  3. apt-get clean and apt-get autoremove. ...
  4. Cleaning the Cache with bleachbit. ...
  5. Installing ncdu. ...
  6. Using ncdu. ...
  7. Cleaning Log Files. ...
  8. Clearing Thumbnail Cache.
Jun 1, 2024

How to check swap memory in Linux? ›

How you can view the amount of swap space on your Linux system? You can use the swapon –show command to view the swap space on your system. Another useful command is the free command that displays both swap space and memory usage. With -m, the results are displayed in MBs instead of KBs.

Why is swap memory high? ›

A higher percentage of swap use is normal when provisioned modules make heavy use of the disk. High swap usage must be a sign that the system experiences memory pressure. This alarm gets generated whenever available swap memory on the Virtual Machine (VM) is lower than the configured threshold value.

How do I reduce swap partition size in Linux? ›

If you want to change the size of your swap file (which is 1GB by default on Ploi) just follow the following steps.
  1. Turn off all running swap processes: swapoff -a.
  2. Resize swap fallocate -l 1G /swapfile (change 1G to the gigabyte size you want it to be)
  3. CHMOD swap: chmod 600 /swapfile.

How much swap memory is enough? ›

If there is less than 1GB of RAM, the swap size should be at least the amount of RAM and, at most, double the amount of RAM. If there is more than 1GB of RAM, the swap size should be at least the square root of the RAM amount and, at most, double the RAM amount.

How to empty the cache on Linux? ›

sysctl -w vm. drop_caches=3 – This command is used to clear all caches, including the page cache, dentries, and inodes. sysctl -w vm. drop_caches=0 – This command is used to reset the drop_caches parameter, effectively stopping any further clearing of the cache.

How do I run out of swap memory? ›

The problem of running out of swap depends upon whether you are really swapping or paging. In a virtual memory system all user user memory has to be mapped to a file. Often this is a combination of the executable file and the page file.

How to clear memory usage in Linux? ›

How to Clear Ram Cache in Linux?
  1. First choice you have is to Clear PageCache only – # sync; echo 1 > /proc/sys/vm/drop_caches.
  2. The second option you have is to Clear dentries and inodes – # sync; echo 2 > /proc/sys/vm/drop_caches.
  3. The third and the last choice you get is to Clear pagecache, dentries, and inodes –
Apr 26, 2022

How to remove swap files in Linux? ›

Use the swap -d command to remove swap space. The swap file name is removed from the list so that it is no longer available for swapping. The file itself is not deleted. Edit the /etc/vfstab file and delete the entry for the swap file.

How to disable swap memory in Linux? ›

Or disable all swaps from /proc/swaps. To check if the swap area has been disabled, run the free command. To permanently disable Linux swap space, open the /etc/fstab file, search for a swap line and add a # (hashtag) sign in front of the line to comment on the entire line, as shown in the screenshot below.

How do I free up unused space in Linux? ›

In this section, we'll go over the different steps to clean up our Linux system.
  1. 2.1. Check for Largest Directories and Files With du. ...
  2. 2.2. Find out the Disk Usage With ncdu. ...
  3. 2.3. Remove Cached Files. ...
  4. 2.4. Remove Old Logs. ...
  5. 2.5. Remove Temporary Files. ...
  6. 2.6. Remove Unused Dependencies. ...
  7. 2.7.
Jul 19, 2024

How do you clear memory space? ›

Here are six steps you can follow to free up space on your phone:
  1. Check your storage use.
  2. Delete or offload apps you haven't been using.
  3. Optimize photo storage.
  4. Clear your cache.
  5. Delete downloads.
  6. Delete photos sent and received in message apps.
Feb 26, 2024

How do I free up space on my Linux filesystem? ›

Freeing disk space on your Linux server
  1. Get to the root of your machine by running cd /
  2. Run sudo du -h --max-depth=1.
  3. Note which directories are using a lot of disk space.
  4. cd into one of the big directories.
  5. Run ls -l to see which files are using a lot of space. Delete any you don't need.
  6. Repeat steps 2 to 5.

How to clear RAM cache in Linux terminal? ›

For clearing cache memory in Linux, we can use the /proc/sys/vm/drop_caches file, which is a special file in the proc filesystem of Linux systems. It allows users to control and manage the kernel's behavior related to cache management.

Top Articles
6 Steps to Improved Performance - Engaged
What is ray tracing?
Kmart near me - Perth, WA
Cold Air Intake - High-flow, Roto-mold Tube - TOYOTA TACOMA V6-4.0
Mcfarland Usa 123Movies
Missing 2023 Showtimes Near Cinemark West Springfield 15 And Xd
Comforting Nectar Bee Swarm
Insidious 5 Showtimes Near Cinemark Tinseltown 290 And Xd
Activities and Experiments to Explore Photosynthesis in the Classroom - Project Learning Tree
How to know if a financial advisor is good?
Poe Pohx Profile
Needle Nose Peterbilt For Sale Craigslist
Koop hier ‘verloren pakketten’, een nieuwe Italiaanse zaak en dit wil je ook even weten - indebuurt Utrecht
Guidewheel lands $9M Series A-1 for SaaS that boosts manufacturing and trims carbon emissions | TechCrunch
Guilford County | NCpedia
Craigslist Mpls Cars And Trucks
Eka Vore Portal
Viha Email Login
Hollywood Bowl Section H
Stardew Expanded Wiki
Cta Bus Tracker 77
Milanka Kudel Telegram
Sussur Bloom locations and uses in Baldur's Gate 3
Craigslistodessa
Craigslist Wilkes Barre Pa Pets
Unreasonable Zen Riddle Crossword
Rural King Credit Card Minimum Credit Score
Dairy Queen Lobby Hours
Greater Orangeburg
Chadrad Swap Shop
Utexas Baseball Schedule 2023
Craigslist Free Puppy
Workboy Kennel
Quality Tire Denver City Texas
24 slang words teens and Gen Zers are using in 2020, and what they really mean
Old Peterbilt For Sale Craigslist
Ni Hao Kai Lan Rule 34
New Gold Lee
Dmitri Wartranslated
Yogu Cheshire
Ukraine-Krieg - Militärexperte: "Momentum bei den Russen"
Electric Toothbrush Feature Crossword
Sdn Fertitta 2024
Locate phone number
Walmart Careers Stocker
FactoryEye | Enabling data-driven smart manufacturing
Latina Webcam Lesbian
Rise Meadville Reviews
Access One Ummc
211475039
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 6147

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.