Free unused memory - page cache, dentries and inodes cache

Linux (and any other OS) tries to cache disk operations to reduce the load on disk itself. When a file is read from disk for first time it's cached to RAM. Next time the file is requested, it's loaded from the RAM. This is really useful for frequently used files, as access times to RAM are significantly lower than to disk. This buffer system is called page cache.

When operating on large amount of files (10000+) cache size can grow up to hundreds of MB. As page cache priority is lower than a processes memory priority, it is freed from time to time. Or You can free it on your own.

# flush file system buffers (cache > disk)
sudo sync
# free page cache
sudo -s "echo 1 > /proc/sys/vm/drop_caches"
# free inodes and dentries cache
sudo -s "echo 2 > /proc/sys/vm/drop_caches"
# free page cache, inodes and dentries cache
sudo -s "echo 3 > /proc/sys/vm/drop_caches"

Note the sync command. Page cache also caches write operations. When you save a file that's already caches, content is saved into the cache and real disk write operation happens later in some batch. Such cached object/page is called dirty and can't be freed. To force the disk write of all dirty pages and make them freeable, use sync before.

No comments:

Post a Comment