Since a tmpfs exists only in memory, can it become fragmented like memory can? Are there mechanisms to automatically defragment the memory, and thus increase - even slightly - the access speed of stored files?


Solution 1:

As far as I know there are no ways to defragment a tmpfs filesystem (at least not on FreeBSD, Linux or Solaris).

Depending on the underlying implementation it's possible for tmpfs to become fragmented (e.g. some tmpfs implementations aren't in RAM - they chew up swap space: Depending on the allocation algorithm the swap space allocated could be fragmented and show a noticeable speed drop).

It's also possible for tmpfs in RAM to become "fragmented", but this isn't a practical concern: RAM is "fast enough" that you'd waste more CPU time trying to "defragment" it than you would lose accessing non-contiguous sections of RAM (no rotational delay to worry about, and the extra overhead for the MMU or equivalent to access non-contiguous sections of RAM versus a contiguous series of pages is small enough that you can probably neglect it.

Frankly if you're concerned about this level of performance you don't want to be using tmpfs anyway because the overhead of the VFS layer to make RAM look like a filesystem is many orders of magnitude worse than any memory fragmentation issues you may encounter (context switching, etc.).

Solution 2:

All tmpfs implementations are backed by virtual memory, not physical one. The question then doesn't make much sense as contiguous areas of virtual memory are very unlikely to be contiguous at the physical level and might not even present on RAM depending on the demand.