Is it possible to selectively clear content from a particular filesystem, or file/directory within a filesystem, under Linux.

This can be done globally with sync; echo 3 > /proc/sys/vm/drop_caches and for a whole filesystem by temporarily umounting and remounting it, but I've not come across a more fine grained option.

Why would I want this? If there had been a bunch of block cached due to I/O from a process that is going to repeat later, and then a one-off (or regular but not frequent) task is run that results in a chunk of data getting cached (but not enough to push the first lot out), I might like to tell the kernel that I no longer care about the second lot of blocks so that if a third chunk of I/O occurs it gets nixed not the first lot that was loaded from disc - so the soon-to-repeat task isn't slowed down on next run due to the one-off task.

Alternately, is there a way to start a process in such a way as to tell the kernel "this is a one off, don't go out of your way to cache what it touches after it finishes".

This could of course be me wanting to waste time micromanaging something that I should leave well along and let the kernel get on with without interference...


The system call for this in Linux is fadvise where you can suggest that different files be cached or not cached, and also suggest the type of read pattern (ie sequential or random). Someone wrote a command line wrapper for this in perl here. The two options you are interested in I think are:

        Announces the expectation that file content won't be needed in
        the near future.

        Under Linux 2.6.9, this is known to cause the file content to be
        evicted from the buffer-cache immediately.

        Note that this option first causes an fsync of the file.

        Announces the expectation that file content won't be reused in
        the near future.

Why would you do this? You will likely want the data cached during the run. Once the run is done the cached data will age out of memory as newer requests come in. In the mean time it will sit in otherwise unused memory.

Doing raw I/O will likely keep the buffering to the drives internal cache. This can result in decreased performance of the application, as you loose the O/S caching and subsequent speed up. When I first started worrying about relative access speed, it was on the order of 100/1 in favor of memory. The benefit of caching vs I/O has been steadily increasing since.

Any security considerations are a moot point as the disk image will likely be available.

  • But it will age out of memory after data from other activity that is more likely to be repeated again. The kernel has no way of knowing that this data is more eligible to be dropped before the other unless something can tell it so. – David Spillett Jul 5 '10 at 8:30
  • If the other data is being used it won't age out. The standard cache release mechanism is Least Recently Used, so anything aged out will not have been used since the data you want to force out of cache. – BillThor Jul 10 '10 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.