How is the memory usage rate calculated in Linux?

You can calculate the Linux memory usage rate by dividing “used memory/total memory”. You can use the following command to get information on used and total memory:

free -m

This command will display memory usage in MB. “used” shows the amount of memory already in use, while “total” indicates the total memory available.

Calculate the memory usage rate using the following formula:

内存使用率 = (已使用内存 / 总内存) * 100

This calculation process can be wrapped into a script, and executed using the following command:

#!/bin/bash

mem_info=$(free -m | grep Mem)
used_mem=$(echo $mem_info | awk '{print $3}')
total_mem=$(echo $mem_info | awk '{print $2}')

mem_usage=$(awk "BEGIN {printf \"%.2f\", $used_mem/$total_mem * 100}")
echo "内存使用率为: $mem_usage%"

By running this script, you can obtain the current memory usage rate.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds