I'm able to auto detect RAM in GB as below and round off to the nearest integer:
printf "%.f\n" $(grep MemTotal /proc/meminfo | awk '$3=="kB"{$2=$2/1024^2;$3="GB";} 1' | awk '{print $2}')
Output:
4
I multiply by 2 to determine the required swap as 8GB
ans=`expr $(printf "%.f\n" $(grep MemTotal /proc/meminfo | awk '$3=="kB"{$2=$2/1024^2;$3="GB";} 1' | awk '{print $2}')) \* 2`
echo "$ans"G
Output:
8G
With the below commands I try to create 8GB swap memory.
echo "Creating $ans GB swap memory"
sudo dd if=/dev/zero of=/swapfile bs="$ans"G count=1048576
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
However, I get the below error:
Creating 8 GB swap memory
dd: memory exhausted by input buffer of size 8589934592 bytes (8.0 GiB)
mkswap: error: swap area needs to be at least 40 KiB
swapon: /swapfile: read swap header failed.
Can you please suggest and help me auto-create swap memory which Ideally should be double of that of the RAM.
System details:
root@DKERP:~# uname -a
Linux DKERP 5.4.0-124-generic #140-Ubuntu SMP Thu Aug 4 02:23:37 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
root@DKERP:~# free -g -h -t
total used free shared buff/cache available
Mem: 3.8Gi 1.0Gi 207Mi 54Mi 2.6Gi 2.5Gi
Swap: 0B 0B 0B
Total: 3.8Gi 1.0Gi 207Mi
bs=1M
and setcount
to the number of megabytes you want the swapfile to be – abligh Sep 05 '22 at 06:27