$ cat /proc/mounts | egrep ' /tmp '
tmpfs /tmp tmpfs rw,nosuid,nodev,relatime 0 0
$ dd if=/dev/zero bs=1M count=3000 of=/tmp/q
3000+0 records in
3000+0 records out
3145728000 bytes (3.1 GB) copied, 1.04961 s, 3.0 GB/s
$ time rm /tmp/q

real    0m0.296s
user    0m0.000s
sys     0m0.290s

Why not 0.000s? There is no disk involved, just marking that memory is not used anymore.

  • @mdpc he measuring rm only, tho – PSkocik Feb 1 '16 at 20:46
  • 3
    Still the rm command does do some work as well as the internal device drivers and mamagement of the memory filesystem....so yes there is still SOME real work being done even if it is small. You might be interested in doing an strace and seeing all the system calls that are being made. – mdpc Feb 1 '16 at 20:48

The "marking that memory as unused" is a function of how much work the unlinkat(2) system call has to do, which in turn scales linearly with the size of the file. For a default tmpfs on a RHEL 6 system with ~4G of memory, this can be demonstrated as follows.

$ sudo mkdir /tmpfs; sudo mount -t tmpfs -o size=75% tmpfs /tmpfs; cd /tmpfs
$ dd if=/dev/zero bs=1M of=blah count=2859
$ strace -c rm blah 2>&1 | head -3
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
100.00    0.241964      241964         1           unlinkat
$ for c in 500 1000 1500 2000 2500; do dd if=/dev/zero bs=1M of=blah count=$c 2>/dev/null; echo -n "$c "; strace -c rm blah 2>&1 | awk '/unlinkat/{print $3}'; done
500 53992
1000 88986
1500 135980
2000 174974
2500 222966

As to what the unlinkat(2) system call is doing in particular, that would require digging around in the source code; my guess is that the data structure that represents the file in memory (a linked list?) is being looped over as the file is removed, thus accounting for the linear growth of operation time with file size.

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.