0

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.

1 Answer 1

1

It seems to be a problem of keeping references to open files in Java (Scala), I guess the space of those is not reclaimed. E.g.

$ lsof -p <pid-of-process>

java    377068 user   92r   REG               0,66        84        6 /mnt/ramdisk/ph/ph0.aif (deleted)
java    377068 user   93r   REG               0,66        54        5 /mnt/ramdisk/db/db0.aif (deleted)
java    377068 user   94r   REG               0,66        84        6 /mnt/ramdisk/ph/ph0.aif (deleted)
java    377068 user   95r   REG               0,66   2305024        7 /mnt/ramdisk/tmp/rec.irc
java    377068 user   97r   REG               0,66   2305024        7 /mnt/ramdisk/tmp/rec.irc
java    377068 user   98r   REG               0,66   1495192       13 /mnt/ramdisk/db/db0.aif (deleted)
java    377068 user   99r   REG               0,66    576054       10 /mnt/ramdisk/db/db0.aif (deleted)
java    377068 user  100r   REG               0,66   2304084        9 /mnt/ramdisk/ph/ph1.aif (deleted)
java    377068 user  101r   REG               0,66    576054       10 /mnt/ramdisk/db/db0.aif (deleted)
java    377068 user  102r   REG               0,66    576054       10 /mnt/ramdisk/db/db0.aif (deleted)
java    377068 user  103r   REG               0,66   2305024        7 /mnt/ramdisk/tmp/rec.irc
java    377068 user  104r   REG               0,66    576054       10 /mnt/ramdisk/db/db0.aif (deleted)
java    377068 user  105r   REG               0,66   2305024        7 /mnt/ramdisk/tmp/rec.irc
java    377068 user  106r   REG               0,66    576054       10 /mnt/ramdisk/db/db0.aif (deleted)
java    377068 user  107r   REG               0,66   2305024        7 /mnt/ramdisk/tmp/rec.irc
java    377068 user  108r   REG               0,66   2305024        7 /mnt/ramdisk/tmp/rec.irc
java    377068 user  109r   REG               0,66    576054       10 /mnt/ramdisk/db/db0.aif (deleted)
java    377068 user  110r   REG               0,66   2305024        7 /mnt/ramdisk/tmp/rec.irc
java    377068 user  111r   REG               0,66    576054       10 /mnt/ramdisk/db/db0.aif (deleted)
java    377068 user  112r   REG               0,66   2304084        9 /mnt/ramdisk/ph/ph1.aif (deleted)
java    377068 user  113r   REG               0,66   2304084        9 /mnt/ramdisk/ph/ph1.aif (deleted)
java    377068 user  114r   REG               0,66   2304084        9 /mnt/ramdisk/ph/ph1.aif (deleted)
java    377068 user  115r   REG               0,66   2305024        7 /mnt/ramdisk/tmp/rec.irc
java    377068 user  116r   REG               0,66   2305024        7 /mnt/ramdisk/tmp/rec.irc
java    377068 user  117r   REG               0,66   2515148       12 /mnt/ramdisk/ph/ph0.aif (deleted)
java    377068 user  118r   REG               0,66   1495192       13 /mnt/ramdisk/db/db0.aif (deleted)
java    377068 user  119r   REG               0,66   1495192       13 /mnt/ramdisk/db/db0.aif (deleted)
...

If I add up the sizes (column 7) of all the unique references (column 8) here, I come close to the space reported by df.


Edit: After debugging, I found a location in my code where files are opened but not closed. It seems that, at least when using the RAM disk, even though the files get overwritten, this prevents the backing memory from being freed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.