Clean Swap Memory


I noticed my Ubuntu desktop was using a rather large portion of available memory. I usually have a lot running on my system, multiple terminals, background jobs, etc so this is nothing unusual. Today however I noticed my system was sluggish so I started digging. Memory use was near 100%. I closed all of my programs to see what effect that would have but the memory usage stayed very high ~90%. I started to suspect a memory leak in one of the processes or programs I was running. I really didn’t want to reboot the system since it isn’t a Windows desktop! What to do. I needed to force memory cleanup on the system. How do I analyze the memory usage on a system? I thought I would document a few of the ways to see memory use.

  1. Clean Swap Memory Recovery
  2. Swap Memory Usage
  3. Clean Swap Memory Foam Mattress

Once you are sure that the size of free RAM is bigger than the swap memory in use, you can clear the swap memory by turning it off and on again. Pun aside, that’s the way to do it. Disable all the swap using this command. Cleaning Up Memory Usage. Posted on May 6, 2015 by Saba, Mitch. 83572 Swap: 4093884 0 4093884. Another command that can free up used or cached memory. If you want to clear Swap space, you may like to run the below command. # swapoff -a && swapon -a. Also you may add above command to a cron script above, after understanding all the associated risk. Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.

You can use commands like ‘top’ and ‘vmstat’ to get an idea of what your system is chewing on. Specifically looking at memory I tend to use:

For a more detailed look use:

If you suspect a program of having a leak you can use valgrind to dig even deeper:

‘valgrind’ is great for testing however not to helpful with currently running processes or without some experience.

So you analyze the system and determine there is memory that has not been properly freed, what do you do? You can reboot but that isn’t always an option. You can force clear the cache doing the following:

This frees up unused but claimed memory in Ubuntu a (and most linux flavors). This command won’t affect system stability and performance, it will just clean up memory used by the Linux Kernel on caches. That said I have noticed the system is more responsive (contradiction, you decide). Here is an example of how much memory you can free up with this command:

Another command that can free up used or cached memory (inodes, page cache, and ‘dentries’):


I have not seen any significant difference between the results of this or the first command.

I’ll add updates to this page as I think of them. Good luck for now.


NGINX Config GeneratorClean Swap Memory

Easily configure a performant, secure, and stable NGINX server.

WordPRess Files Removed Now What To Do? Question



I am using serverpilot. 512 MB swap memory is filled with inactivate pages. I want to know how to free swap memory?

Clean Swap Memory Recovery

1) How to clear this ?
2) How to increase Swap memory?
3) I need to login as root or serverpilot?


Swap Memory Usage

NGINX Config Generator

Easily configure a performant, secure, and stable NGINX server.

Clean Swap Memory Foam Mattress

WordPRess Files Removed Now What To Do? Question
  • saurabhJuly 23, 2015

    Hey, here is an article that might help you:

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.