Wednesday, February 08, 2012

Free Memory in Ubuntu

I was so happy to figure this out that I wanted to post it for my own memory and anyone else who was also interested.

On one of my computers, my RAM was being eaten up and I couldn't figure out why.  Running 'top' resulted in the following line:

Mem:    505908k total,   478736k used,    27172k free,    41888k buffers

Out of my half gig of ram, 95% was being used.  Browsing the list of processes didn't give any indication that there were any particular memory hogs.

Running 'free -m' (the -m prints in megabyes instead of the default kilobytes) resulted in the following output:

user$ free -m
             total       used       free     shared    buffers     cached
Mem:           494        469         25          0         41        315
-/+ buffers/cache:        112        381


From that, I was able to discover that the majority of my RAM was tied up in caching data.  Not necessarily bad, since if you have spare RAM lying around, you might as well use it for something, but it was slowing down my SSH terminals to the point where they were effectively unusable.  The previous couple times I had this problem, I had simply rebooted, but that was a hammer=nail solution and I wanted a better one.  I finally found the solution on ubuntu-unleashed.com.
  1. Flush file system buffers for safety
    1. user$ sync
  2. Become root (simply sudo-ing the final command won't work because of the redirection)
    1. user$ sudo bash
  3. Free cached memory
    1. root# echo 1 > /proc/sys/vm/drop_caches
To check that Ubuntu had given back my RAM, 'top' resulted in the following:

Mem:    505908k total,   120384k used,   385524k free,      204k buffers

Additionally, 'free -m' resulted in the following:


user$ free -m
             total       used       free     shared    buffers     cached
Mem:           494        128        365          0          0         24
-/+ buffers/cache:        103        390