在阿里云买了一台 2C2G 的轻量级服务器,运行的 Ubuntu24.04 系统。
在使用 free 查看内存的时候发现只有 1.6G
root@linux:~# free -m
total used free shared buff/cache available
Mem: 1613 972 97 1 702 640
Swap: 4095 295 3800
然后查看内存分配的时候发现确实是分配了 2G 内存
root@linux:~# dmidecode -t memory
# dmidecode 3.5
Getting SMBIOS data from sysfs.
SMBIOS 2.8 present.
Handle 0x1000, DMI type 16, 23 bytes
Physical Memory Array
Location: Other
Use: System Memory
Error Correction Type: Multi-bit ECC
Maximum Capacity: 2 GB
Error Information Handle: Not Provided
Number Of Devices: 1
Handle 0x1100, DMI type 17, 40 bytes
Memory Device
Array Handle: 0x1000
Error Information Handle: Not Provided
Total Width: Unknown
Data Width: Unknown
Size: 2 GB
Form Factor: DIMM
Set: None
Locator: DIMM 0
Bank Locator: Not Specified
Type: RAM
Type Detail: Other
Speed: Unknown
Manufacturer: Alibaba Cloud
Serial Number: Not Specified
Asset Tag: Not Specified
Part Number: Not Specified
Rank: Unknown
Configured Memory Speed: Unknown
Minimum Voltage: Unknown
Maximum Voltage: Unknown
Configured Voltage: Unknown
那么问题来了,缺少的那部分内存哪里去了?
经过一番排查发现是阿里云镜像的crashkernel的配置有问题
root@linux:~# cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-6.8.0-63-generic root=UUID=59667354-7b33-4856-825d-652c4cf5dddb ro vga=792 console=tty0 console=ttyS0,115200n8 net.ifnames=0 noibrs nvme_core.io_timeout=4294967295 nvme_core.admin_timeout=4294967295 iommu=pt crashkernel=0M-2G:0M,2G-4G:256M,4G-64G:384M,64G-:512M crash_kexec_post_notifiers=1
通过命令查看crashkernel的配置,发现阿里云在 2G 内存的机器上会预留 256M 的空间给crashkernel使用。
root@linux:~# cat /proc/iomem | grep -i "crash kernel"
62000000-71ffffff : Crash kernel
crashkernel的作用是在系统崩溃的时候将崩溃的快照进行保留的一块预留内存。
然而给一个 2G 的机器上预留一个 256M 内存是不太合理的。
除非你的机器是做内核调试,或者有非常非常重要的业务在系统崩溃时要保留现场。
但是内核开发者会自己设置这个crashkernel大小,重要的业务肯定不会抠搜搜的只给 2G 内存吧。
查了一下腾讯云机器上的配置,3G以下机器预留 0M 的空间给crashkernel。
root@qq:~# cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-6.8.0-100-generic root=UUID=9842d3d6-a839-4127-bda7-f19137effe71 ro splash=silent showopts crashkernel=0M-3G:0M,3G-8G:192M,8G-16G:256M,16G-64G:384M,64G-128G:512M,128G-:768M net.ifnames=0 biosdevname=0 console=ttyS0,115200 console=tty0 panic=5 intel_idle.max_cstate=1 intel_pstate=disable processor.max_cstate=1 amd_iommu=on iommu=pt
看来在提升用户体验细节上腾讯云还是比阿里云更强一点。
接下来就是如何关掉crashkernel的占用的内存。
修改/etc/default/grub的GRUB_CMDLINE_LINUX_DEFAULT那一行,将crashkernel设置为 no
root@linux:~# cat /etc/default/grub
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
# info -f grub -n 'Simple configuration'
GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=1
GRUB_DISTRIBUTOR=`( . /etc/os-release; echo ${NAME:-Ubuntu} ) 2>/dev/null || echo Ubuntu`
GRUB_CMDLINE_LINUX_DEFAULT=""
#GRUB_CMDLINE_LINUX=" vga=792 console=tty0 console=ttyS0,115200n8 net.ifnames=0 noibrs nvme_core.io_timeout=4294967295 nvme_core.admin_timeout=4294967295 iommu=pt crashkernel=0M-2G:0M,2G-4G:256M,4G-64G:384M,64G-:512M crash_kexec_post_notifiers=1"
GRUB_CMDLINE_LINUX=" vga=792 console=tty0 console=ttyS0,115200n8 net.ifnames=0 noibrs nvme_core.io_timeout=4294967295 nvme_core.admin_timeout=4294967295 iommu=pt crashkernel=no crash_kexec_post_notifiers=1"
更新 grub,并重启服务器
update-grub
reboot
重启后看到内存涨了 256M。
root@linux:~# free -m
total used free shared buff/cache available
Mem: 1869 978 65 2 983 890
Swap: 4095 83 4012
不过依然比腾讯云相同配置的可用内存少,经过对比分析,是被硬件占用,无法释放给用户使用的。
所以由此说明腾讯云的底层优化是要优于阿里云的。