I'm using mmap to create shared memory segments, and I'm wondering if I can pre-create all the segments I'm going to possibly use in /dev/shm without triggering any memory use. The reason I suspect this may be possible is that I know most filesystems have a concept of all-zero pages, and it's possible when you initially grow a file before you do any writes to have the file not really take up space because of these 'hole' pages. But is this true for tmpfs (filesystem for /dev/shm)? Can I go wild making large files in /dev/shm without triggering memory use as long as I don't write to them?
On Linux, the tmpfs
file system supports sparse files. Just resizing the file does not allocate memory (beyond the internal tmpfs
data structures). Just like with regular file systems which support sparse files (files with holes), you either have to actually write data or use fallocate
to allocate backing storage. As far as I can see, this has been this way since the Linux 2.6 days.