# 建立 RAM Disk
* 先確認可用記憶體空間
```
$ free -mh
total used free shared buff/cache available
Mem: 15Gi 6.0Gi 8.0Gi 43Mi 1.6Gi 9.3Gi
Swap: 0B 0B 0B
```
* 建立 RAM Disk 使用目錄
```
$ sudo mkdir /opt/ramdisk
$ sudo chmod 777 /opt/ramdisk
```
* 掛在 3G 的記憶體當成 Disk 使用
```
$ sudo mount -t tmpfs -o size=3G tmpfs /opt/ramdisk/
$ df -h /opt/ramdisk
Filesystem Size Used Avail Use% Mounted on
tmpfs 3.0G 0 3.0G 0% /opt/ramdisk
```
## 測試讀寫速度
### 寫入測試
* 測試在 RAM Disk 寫入一個 2g 的檔案
* 速度為 640 MB/s
```
$ sudo dd count=2k bs=1M if=/dev/zero of=/opt/ramdisk/test2g.img
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 3.35613 s, 640 MB/s
$ ls -alh /opt/ramdisk/test2g.img
-rw-r--r-- 1 root root 2.0G Mar 25 13:53 /opt/ramdisk/test2g.img
```
* 測試在一般 Disk 寫入一個 2g 的檔案
* 速度為 268 MB/s
```
$ sudo dd count=2k bs=1M if=/dev/zero of=/opt/test2g.img
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 8.01034 s, 268 MB/s
$ ls -alh /opt/test2g.img
-rw-r--r-- 1 root root 2.0G Mar 25 13:54 /opt/test2g.img
```
### 讀取測試
* 清理 cache
```
$ echo 3 | sudo tee /proc/sys/vm/drop_caches
```
* 測試在 RAM Disk 讀取一個 2g 的檔案
* 速度為 1.2 GB/s
```
$ sudo dd count=2k bs=1M if=/opt/ramdisk/test2g.img of=/dev/null
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 1.82153 s, 1.2 GB/s
```
* 測試在一般 Disk 讀取一個 2g 的檔案
* 速度為 151 MB/s
```
$ sudo dd count=2k bs=1M if=/opt/test2g.img of=/dev/null
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 14.1823 s, 151 MB/s
```
## 參考文件
https://blog.gtwang.org/linux/linux-ram-disk/