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

6 comments:

Eric Bell said...

Excellent tip, I am still learning my around Ubuntu and memory is being eaten up everywhere. I was wondering how to force the system to recalibrate.

Eric Bell said...

If this is a duplicate posting - sorry but the identity mechanism leaves me in the dark.

This post is very helpful, I am still learning the ins and outs of Ubuntu and I've been wondering how to do just this. Memory is being allocated from boot and I now have a way to recalibrate it the fly. Excellent.

Harvey Ramer said...

Is there a way to configure Ubuntu to flush its own cache when system resources become scarce?

The Writer said...

I haven't explored the possibilities. You obviously could script this, but if you're getting this problem, it probably speaks of some deeper problem in the system.

The Writer said...

I haven't explored the possibilities. You obviously could script this, but if you're getting this problem, it probably speaks of some deeper problem in the system.

The Writer said...

I haven't explored the possibilities. You obviously could script this, but if you're getting this problem, it probably speaks of some deeper problem in the system.