Raspberry Pi - Ramdisk
What is RAM Disk?
A RAM drive (also called a RAM disk) is a block of random-access memory (primary storage or volatile memory) that a computer's software is treating as if the memory were a disk drive (secondary storage). It is sometimes referred to as a virtual RAM drive or software RAM drive to distinguish it from a hardware RAM drive that uses separate hardware containing RAM, which is a type of battery-backed solid-state drive.
From WikiPedia
There are several reasons to use ram disks on the Raspberry Pi.
The Raspberry Pi uses a MicroSD card as a storage device. SD cards have a fixed write life. If you write more than a certain number of times, the life of the SD card will end. This phenomenon is the same for SSDs. To prevent this, you can use a large-capacity SD card. However, if you have to use a low-capacity SD card of 8GB to 16GB due to problems such as cost, excessive write operations shorten the lifespan of the SD card. RamDisk is worth considering if you are running a program that requires frequent writing to temporary files. DRAM has a longer lifespan than SD cards and is hardly affected by the number of writes.
The second reason to use ramdisk is speed. The latest SD cards provide read speed of 100MB/s and write speed of 10 to 30MB/s or higher. However, this speed is also much slower than that of eMMC and SSD storage devices. If the performance degradation occurs seriously due to the file I/O speed, you can consider a ramdisk.
And from Raspberry Pi 4, you can choose from 2GB, 4GB, and 8GB of memory. If you have enough memory left after running your application, you can use ramdisk. However, if you don't have extra memory, you shouldn't use a ramdisk. There will be much more harm than good.
Insufficient information about Raspberry Pi ramdisk
Several internet blogs introduce incorrect ramdisk usage. Although the contents are not completely wrong, there are many contents that cannot implement the high-speed I/O ramdisk we want.
First of all, let's make a ramdisk by publishing it on several blogs. The Raspberry Pi 4B+ 2GB model will be used for testing.
Create a mount point.
sudo mkdir /mnt/ramdisk
sudo nano /etc/fstab
Then add the following line to the fstab file.
tmpfs /mnt/ramdisk tmpfs nodev,nosuid,size=100M 0 0
Then restart the Raspberry Pi.
After rebooting, check the directory using df command.
root@raspberrypi:~# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 15G 3.0G 11G 22% /
devtmpfs 805M 0 805M 0% /dev
tmpfs 934M 0 934M 0% /dev/shm
tmpfs 934M 17M 917M 2% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 934M 0 934M 0% /sys/fs/cgroup
tmpfs 100M 0 100M 0% /mnt/ramdisk
/dev/mmcblk0p1 253M 48M 205M 19% /boot
tmpfs 187M 4.0K 187M 1% /run/user/1000
tmpfs 187M 0 187M 0% /run/user/0
You can see that 200MB of /mnt/ramdisk has been properly created with the tmpfs file system. And if you save the file in the /mnt/ramdisk directory to read and write it, it works normally.
root@raspberrypi:~# echo "Hello World"> /mnt/ramdisk/hello.txt root@raspberrypi:~# ls -al /mnt/ramdisk/ total 8 drwxrwxrwt 2 root root 60 May 16 12:38 . drwxr-xr-x 4 root root 4096 May 16 12:34 .. -rw-r--r-- 1 root root 12 May 16 12:38 hello.txt
Ramdisk on the DRAM or Virtual Memory
Many blog posts have been introduced so far, and this is the end. But let's look a little deeper.
Let's compare the speed with a 90MB write operation.
root@raspberrypi:~# dd if=/dev/zero of=/mnt/ramdisk/temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 0.350898 s, 269 MB/s root@raspberrypi:~# dd if=/dev/zero of=/mnt/ramdisk/temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 0.294689 s, 320 MB/s root@raspberrypi:~# dd if=/dev/zero of=/mnt/ramdisk/temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 0.295135 s, 320 MB/s root@raspberrypi:~# dd if=/dev/zero of=/mnt/ramdisk/temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 0.299936 s, 315 MB/s
This time, let's do the same in a non-ramdisk directory.
root@raspberrypi:~# dd if=/dev/zero of=./temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 5.85062 s, 16.1 MB/s root@raspberrypi:~# dd if=/dev/zero of=./temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 2.5143 s, 37.5 MB/s root@raspberrypi:~# dd if=/dev/zero of=./temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 1.87658 s, 50.3 MB/s root@raspberrypi:~# dd if=/dev/zero of=./temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 2.44773 s, 38.6 MB/s
The writing speed of ramdisk is about 10 times faster than non ramdisk.
Now let's check the memory with the free command.
root@raspberrypi:~# free
total used free shared buff/cache available
Mem: 1911320 133572 1346356 48868 431392 1645916
Swap: 102396 0 102396
In addition to 2GB of DRAM, it can be seen that there is about 100MB of swap memory. The swap memory uses a part of the disk like memory so that it can be used in case of insufficient RAM. The disk in the Raspberry Pi is an SD card. Therefore, swap memory is the same as using an SD card.
It is up to the operating system to decide whether to use swap memory or ram. Therefore, we recommend removing this swap usage to safely use only the DRAM space.
Warning : In the case of insufficient memory, swap memory is absolutely necessary. Therefore, follow the procedure below to remove swap only if there is enough free memory.
RamDisk on the RAM Memory(Not Virtual Memory)
Now remove the swap as described above and modify the ramdisk to work only with RAM.
root@raspberrypi:~# free total used free shared buff/cache available Mem: 1911320 133572 1346356 48868 431392 1645916 Swap: 102396 0 102396 root@raspberrypi:~# dphys-swapfile swapoff root@raspberrypi:~# free total used free shared buff/cache available Mem: 1911320 133632 1346108 48868 431580 1645856 Swap: 0 0 0
And completely remove the swap configuration. Now, even if you reboot, no swap will be created.
root@raspberrypi:~# dphys-swapfile uninstall root@raspberrypi:~# update-rc.d dphys-swapfile remove root@raspberrypi:~# apt-get remove dphys-swapfile -y Reading package lists... Done Building dependency tree Reading state information... Done The following package was automatically installed and is no longer required: dc Use 'apt autoremove' to remove it. The following packages will be REMOVED: dphys-swapfile 0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded. After this operation, 67.6 kB disk space will be freed. (Reading database ... 98609 files and directories currently installed.) Removing dphys-swapfile (20100506-5+rpt2) ... Processing triggers for man-db (2.8.5-2) ...
Finally, add the noatime setting to the fstab file like this.
root@raspberrypi:~# cat /etc/fstab proc /proc proc defaults 0 0 PARTUUID=e4a133db-01 /boot vfat defaults 0 2 PARTUUID=e4a133db-02 / ext4 defaults,noatime 0 1 tmpfs /mnt/ramdisk tmpfs defaults,noatime,nodev,nosuid,size=50M 0 0
Then reboot the system and check the IO speed again.
root@raspberrypi:~# dd if=/dev/zero of=/mnt/ramdisk/temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 0.300046 s, 315 MB/s root@raspberrypi:~# dd if=/dev/zero of=/mnt/ramdisk/temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 0.295019 s, 320 MB/s root@raspberrypi:~# dd if=/dev/zero of=/mnt/ramdisk/temp.txt bs=1M count=90 90+0 records in 90+0 records out 94371840 bytes (94 MB, 90 MiB) copied, 0.293828 s, 321 MB/s
There is no difference in writing speed. However, this time, since the swap file has been completely removed, you can always be guaranteed to run in RAM.
Wrapping up
Ramdisk can be guaranteed a fast write speed and can extend the life of SD card, so it can be useful if you frequently write high speed disks. However, ramdisk uses a temporary file system (tmpfs). Therefore, when rebooting, all the files stored in the ramdisk are erased. Therefore, it should be used only for temporary files, and important files should not be stored on the ramdisk.
You can check out the details in the following YouTube video.
댓글
댓글 쓰기