
Would you like to know how to release empty swap space en Linux? Certain applications can be very greedy on the memory resource, Linux then starts using your swap space (SWAP) but it happens that this is not emptied correctly and it seems to be always used causing performance problems related to hard disk accesses.
SWAP swap space in Linux can be enabled/disabled and most of the time, you can empty the SWAP space without having to reboot the system.
Freeing up empty swap space (swap) Methods
To get information, we can easily check the SWAP swap space used on the system with the following command:
swapon -s
The column «Used» will inform us about the current use of the available SWAP memory. If this rate remains too high for too long, slowdowns in access to the hard disk on which the SWAP partition or file is present may be observed.
Here you can read about: There Are Not Enough Memory Resources Available to Process This Command Error
Method 1: Clear empty swap space
To reboot and reset the SWAP we use the following command:
swapoff -a && swapon -a
We deactivate («swapoff») and then we reactivate («swapon«) all partitions or SWAP files to be emptied and reset properly. Disabling or re-enabling everything at once is done with the » option -a " what does it mean "all«.
Method 2: Free up empty swap space in the frequent error message
If your memory is used heavily during this manipulation, you may receive the following error message when running the command » swapoff -a «:
swapoff: /dev/hda5: unable to allocate memory
This means that the memory currently used in the SWAP cannot be swapped to RAM because there is not enough available. Therefore, it is necessary to ensure before running the command that the RAM can have enough free space to receive the used SWAP space.
How to clear swap memory in Linux or free up empty swap space
Got kinky? Learn how to set proper swap settings in Linux and wipe your swap content.
Swap memory is usually a “set it and forget it«. Most enterprise environments have swap built into the systems, and these memory caches are not touched unless there is an apparent lack of available memory or if a server fails due to the killer OOM (out of memory) error.
However, there is one niche situation that may result in an administrator needing to delete the swap from the system manually. If that is the situation you find yourself in, you have come to the right place. This article is a discussion of this situation and the required solution.
Are you 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 system's "swap."
Yes, you read that right… swapping. So now that you know the lingo, you’re ready to explore what it all means. Swappiness refers to the kernel parameter responsible for how much and how often the system moves data from RAM to swap memory.
The default value for swap is 60, however you can manually set it anywhere between 0-100. Small values cause a small amount of swapping to occur, while high values can cause very aggressive swapping.
A value of zero causes no swapping to occur, so if you want to minimize the empty swap space to its lowest possible value without turning off, you must set it to at least one.
If you want to change your system's ability to free up free swap space, the procedure is very simple. You can check your current swap settings by running the following command:
- $cat/proc/sys/vm/swappiness
It should look something like this:
Now, you can see that the system has a swap value of 30. To modify the value, you need to use the following:
$ sudo sysctl vm.swappiness = x
(Where x is the exchange value you want to set)
sysctl output vm.swappiness = 60
To verify the value you set, simply place the swap file you looked at earlier to find out the original value. Now that you understand the underlying parameters that control swap behavior on our system, you are ready to learn how to clear that memory, should the situation arise. For the first time in your life terminal, things are going to be easy here.
Clearing empty swap space
To clear swap on your system, you simply need to unplug swap. This moves all data from swap to RAM. It also means you need to make sure you have the RAM to support this operation. An easy way to do this is to run 'free -m' to see what is used in swap and RAM.
You may also be interested in reading about: How to Create a Bootable USB Flash Drive
Once you turn it off, you can wait an arbitrary amount of time (30 seconds or so) for the operation to complete and then turn the swap back on.
This clears the swap cache and re-enables it. Here are all the commands that you will need!
- Check space: # free -m
- Disable sharing: # swapoff -a
- Wait approximately 30 seconds
- (use free -m to see the amount of used/available swap decrease with There)
- Enable swap: # swapon -a
Conclusion
Hopefully, this quick tip will help you clear free swap space from your system if you ever find yourself in need of such a solution. We hope we have helped you with this information.
My name is Javier Chirinos and I am passionate about technology. Ever since I can remember, I have been interested in computers and video games, and that passion has turned into a job.
I have been publishing about technology and gadgets on the Internet for over 15 years, especially in mundobytes.com
I am also an expert in online marketing and communication and have knowledge in WordPress development.