4

On Linux, when I create a new file and open it, an entry about it is created in the dcache's hash table, right? How long does that entry stay there? Does it get removed when nothing is currently using the file anymore, or does it stay there until the cache is full and the kernel decides to remove things from it to create space for new dcache entries?

If the latter is the case, how exactly does the kernel determine which entries to remove when the dcache is full?

(References to the kernel source would be nice.)

thejh
  • 343

1 Answers1

4

Q1: On Linux, when I create a new file and open it, an entry about it is created in the dcache's hash table, right?

Correct.

Q2: How long does that entry stay there?

Until the space they're occupying is needed for some other purpose.

Q3: If the latter is the case, how exactly does the kernel determine which entries to remove when the dcache is full?

From the Linux kernel documentation on virtual memory (VM):

excerpt

vfs_cache_pressure

Controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects.

At the default value of vfs_cache_pressure=100 the kernel will attempt to reclaim dentries and inodes at a "fair" rate with respect to pagecache and swapcache reclaim. Decreasing vfs_cache_pressure causes the kernel to prefer to retain dentry and inode caches. When vfs_cache_pressure=0, the kernel will never reclaim dentries and inodes due to memory pressure and this can easily lead to out-of-memory conditions. Increasing vfs_cache_pressure beyond 100 causes the kernel to prefer to reclaim dentries and inodes.

So to answer your general question, dentries will remain in cache until the space they're occupying is needed by something else

Additional notes

This philosophy is permeated throughout the kernel. There is no reason to free up RAM, until their's an actual need to do so. If you'd like to analyze the contents of the cache or drop the dentries all together take a look at his Q&A titled: Are there any ways or tools to dump the memory cache and buffer?.

slm
  • 369,824