Dropping Cached Memory on FreeBSD

To start, CentOS is a Linux Operating System. Linux is completely different from FreeBSD.

You really don't need to free up that cached memory, likely you don't need to on your Cent box, but it does depend a bit on what you're running on it. You should only ever mess with memory management when you have a really specific and good reason to do so.

The one and only reason you would want to do this on a production box is if you have an application the bases it's own memory usage on the amount of free memory. I do not know of any *nix programs that do this, but it's possible they're out there. Even if this is the case, you should be employing intelligent memory limiting on the system caches, not flushing them manually or periodically.

The only other common reason to flush the caches is for benchmarking and testing purposes.

If one of the above two do not apply, do not flush your caches.

Update:
In addition to the comments below, let me hammer the performance difference home with a simple test.

dd if=/dev/zero of=/path/to/test.1g bs=1m count=1024
1073741824 bytes transferred in 20.998713 secs (51133697 bytes/sec)

dd if=/path/to/test.1g of=/dev/null bs=1m
1073741824 bytes transferred in 4.496601 secs (238789654 bytes/sec)

dd if=/path/to/test.1g of=/dev/null bs=1m
1073741824 bytes transferred in 1.071374 secs (1002210138 bytes/sec)

The first time reading the test file nothing was cached; the second time it was already in the cache, so the read operation completed four times faster. In a typical server 90% of the reads are to 1% of the files/data on disk. If most of that 1% can stay in cached memory, disks reads will generally be 4x faster (on my server at least).


For those need dropping cache for testing reasons, it looks like there isn't a straight forward way for dropping caches in FreeBSD.

Usually os cache is cache of file system files (or mmaped files). Cache of this files can be cleared by unmounting the corresponding partition.

Source: http://unix.derkeiler.com/Mailing-Lists/FreeBSD/hackers/2010-03/msg00244.html


Just to clarify the unmount solution: an attempt to unmount the filesystem flushes and clears the cache for that filesystem, even though the unmount itself does NOT have to succeed.

cd /mnt
umount /mnt

The umount will fail since the current working directory keeps it busy, but any cached data from /mnt is anyway cleared.

Example:

root:/usr/home/user> dd if=test of=/dev/null bs=1048576 count=1000
1048576000 bytes transferred in 10.012924 secs (104722255 bytes/sec)

root:/usr/home/user> dd if=test of=/dev/null bs=1048576 count=1000
1048576000 bytes transferred in 0.290137 secs (3614071699 bytes/sec)

root:/usr/home/user> umount /usr
umount: unmount of /usr failed: Device busy

root:/usr/home/user> dd if=test of=/dev/null bs=1048576 count=1000
1048576000 bytes transferred in 10.149973 secs (103308253 bytes/sec)

Tags:

Freebsd