I am trying to use a RAM disk on a Pi 4, 4GB, to speed up processing and avoid writing a lot to the SD card. But somehow my application fills up the RAM disk until a java.io.IOException: No space left on device is hit. I am only (re-writing) four files as they grow over time, but they should never exceed a fraction of the allocated space. For instance:
$ sudo mkdir /mnt/ramdisk
$ sudo mount -t tmpfs -o size=256m tmpfs /mnt/ramdisk
This gives a me an empty RAM disk of 256 MB:
$ df | grep ramdisk
tmpfs 262144 0 262144 0% /mnt/ramdisk
After a while the app hits 100%:
$ df | grep ramdisk
tmpfs 262144 260616 1528 100% /mnt/ramdisk
however, the files in the RAM disk only take up around 45 MB:
$ ls -laR /mnt/ramdisk/
/mnt/ramdisk/:
total 4
drwxrwxrwt 5 root root 100 Nov 13 14:23 .
drwxr-xr-x 3 root root 4096 Nov 13 00:16 ..
drwxr-xr-x 2 pi pi 80 Nov 13 14:56 db
drwxr-xr-x 2 pi pi 80 Nov 13 14:56 ph
drwxr-xr-x 2 pi pi 60 Nov 13 14:44 tmp
/mnt/ramdisk/db:
total 30580
drwxr-xr-x 2 pi pi 80 Nov 13 14:56 .
drwxrwxrwt 5 root root 100 Nov 13 14:23 ..
-rw-r--r-- 1 pi pi 15584348 Nov 13 14:56 db0.aif
-rw-r--r-- 1 pi pi 15728348 Nov 13 14:56 db1.aif
/mnt/ramdisk/ph:
total 13300
drwxr-xr-x 2 pi pi 80 Nov 13 14:56 .
drwxrwxrwt 5 root root 100 Nov 13 14:23 ..
-rw-r--r-- 1 pi pi 6842812 Nov 13 14:56 ph0.aif
-rw-r--r-- 1 pi pi 6774716 Nov 13 14:55 ph1.aif
/mnt/ramdisk/tmp:
total 2252
drwxr-xr-x 2 pi pi 60 Nov 13 14:44 .
drwxrwxrwt 5 root root 100 Nov 13 14:23 ..
-rw-r--r-- 1 pi pi 2305024 Nov 13 14:56 rec.irc
(15584348+15728348+6842812+6774716+2305024)/1024.0/1024 // 45 MB
Even taking a bit of overhead into account, how can the disk be full although the contents are less than 20% of the nominal capacity? And more importantly, how can I fix this? I cannot increase the allocated RAM much more than 256 MB, in fact 64 MB should be sufficient for the application.
Here is another approach, comparing df and du, the more the process goes on, the greater the discrepancy:
$ df -h | grep ramdisk
tmpfs 256M 35M 222M 14% /mnt/ramdisk
$ du -h /mnt/ramdisk/
2.2M /mnt/ramdisk/tmp
11M /mnt/ramdisk/ph
6.6M /mnt/ramdisk/db
20M /mnt/ramdisk/
I wouldn't mind if df as "off", but the application crashes when df reports 100% used. Interestingly, when I kill the app, the sizes start to line up again. So this looks like a "caching problem" or something related.