买了台腾讯云轻量应用服务器,用来做测评,同时也记录下针对腾讯云轻量应用服务器的内存使用优化。
系统级别的内存优化
通过命令发现4G的机器只有分配了3694M内存,那么,剩余的306M的内存去哪里了呢?
free -m total used free shared buff/cache available Mem: 3694 840 1990 0 862 2602 Swap: 3999 286 3713
找出少掉的内存
1、底层硬件信息
使用命令 dmidecode 查看底层硬件信息,可以看到这台轻量云 4G 内存的服务器是分配足了 4096M 内存的。
dmidecode # dmidecode 3.2 Getting SMBIOS data from sysfs. SMBIOS 2.8 present. 10 structures occupying 431 bytes. Table at 0x000F6A00. Handle 0x0000, DMI type 0, 24 bytes BIOS Information Vendor: SeaBIOS Version: seabios-1.9.1-qemu-project.org Release Date: 04/01/2014 Address: 0xE8000 Runtime Size: 96 kB ROM Size: 64 kB Characteristics: BIOS characteristics not supported Targeted content distribution is supported BIOS Revision: 0.0 Handle 0x0100, DMI type 1, 27 bytes System Information Manufacturer: Tencent Cloud Product Name: CVM Version: 3.0 Serial Number: 31b2f58d-e972-49c8-9b91-a7d3eef36f8b UUID: 31b2f58d-e972-49c8-9b91-a7d3eef36f8b Wake-up Type: Power Switch SKU Number: Not Specified Family: Smdbmds Handle 0x0300, DMI type 3, 21 bytes Chassis Information Manufacturer: Smdbmds Type: Other Lock: Not Present Version: 3.0 Serial Number: Not Specified Asset Tag: Not Specified Boot-up State: Safe Power Supply State: Safe Thermal State: Safe Security Status: Unknown OEM Information: 0x00000000 Height: Unspecified Number Of Power Cords: Unspecified Contained Elements: 0 Handle 0x0400, DMI type 4, 42 bytes Processor Information Socket Designation: CPU 0 Type: Central Processor Family: Other Manufacturer: Smdbmds ID: E3 06 05 00 FF FB 8B 0F Version: 3.0 Voltage: Unknown External Clock: Unknown Max Speed: 2000 MHz Current Speed: 2000 MHz Status: Populated, Enabled Upgrade: Other L1 Cache Handle: Not Provided L2 Cache Handle: Not Provided L3 Cache Handle: Not Provided Serial Number: Not Specified Asset Tag: Not Specified Part Number: Not Specified Core Count: 2 Core Enabled: 2 Thread Count: 1 Characteristics: None Handle 0x1000, DMI type 16, 23 bytes Physical Memory Array Location: Other Use: System Memory Error Correction Type: Multi-bit ECC Maximum Capacity: 4 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: 4096 MB //这里 Form Factor: DIMM Set: None Locator: DIMM 0 Bank Locator: Not Specified Type: RAM Type Detail: Other Speed: Unknown Manufacturer: Smdbmds 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 Handle 0x1300, DMI type 19, 31 bytes Memory Array Mapped Address Starting Address: 0x00000000000 Ending Address: 0x000BFFFFFFF Range Size: 3 GB Physical Array Handle: 0x1000 Partition Width: 1 Handle 0x1301, DMI type 19, 31 bytes Memory Array Mapped Address Starting Address: 0x00100000000 Ending Address: 0x0013FFFFFFF Range Size: 1 GB Physical Array Handle: 0x1000 Partition Width: 1 Handle 0x2000, DMI type 32, 11 bytes System Boot Information Status: No errors detected Handle 0x7F00, DMI type 127, 4 bytes End Of Table
但是为什么使用 free 命令看到的内存变少了呢?查阅资料找到了答案:free 命令看到的是用户系统管理的内存,这部分内存是不包括 OS 内核一些基本数据结构消耗以及内核预留内存的。
2.内核预留内存
使用命令 dmesg | grep -i memory 查看内核预留内存,注意标注部分:
dmesg | grep -i memory [ 0.000000] Base memory trampoline at [ffff926fc0099000] 99000 size 24576 [ 0.000000] Reserving 256MB of memory at 592MB for crashkernel (System RAM: 4095MB) //预留 [ 0.000000] Early memory node ranges [ 0.000000] PM: Registered nosave memory: [mem 0x0009f000-0x0009ffff] [ 0.000000] PM: Registered nosave memory: [mem 0x000a0000-0x000effff] [ 0.000000] PM: Registered nosave memory: [mem 0x000f0000-0x000fffff] [ 0.000000] PM: Registered nosave memory: [mem 0xbffde000-0xbfffffff] [ 0.000000] PM: Registered nosave memory: [mem 0xc0000000-0xfeffbfff] [ 0.000000] PM: Registered nosave memory: [mem 0xfeffc000-0xfeffffff] [ 0.000000] PM: Registered nosave memory: [mem 0xff000000-0xfffbffff] [ 0.000000] PM: Registered nosave memory: [mem 0xfffc0000-0xffffffff] [ 0.000000] Memory: 3760760k/5242880k available (7988k kernel code, 1049104k absent, 433016k reserved, 5756k data, 2176k init) [ 0.000000] please try 'cgroup_disable=memory' option if you don't want memory cgroups [ 0.372359] Initializing cgroup subsys memory [ 0.566477] x86/mm: Memory block size: 128MB [ 1.218403] Freeing initrd memory: 19008k freed [ 1.339414] Non-volatile memory driver v1.3 [ 1.342119] crash memory driver: version 1.1 [ 1.483157] Freeing unused kernel memory: 2176k freed [ 1.486966] Freeing unused kernel memory: 192k freed [ 1.489185] Freeing unused kernel memory: 528k freed [ 1.928677] [TTM] Zone kernel: Available graphics memory: 1891346 kiB
看到系统为 crashkernel 预留了 256MB 的内存,很明显这就是导致我们“内存不足量”的罪魁祸首。
腾讯云提供的 Debian、Ubuntu或者Centos 系统镜像都默认开启了 kdump 服务,kdump 是获取崩溃的 Linux 内核转储的一种方法。而上面提到的“为 crashkernel 预留的内存”就是用于这一服务的,也就是导致我们“内存不足量”的元凶。
既然问题是由于系统为 kdump 服务预留了一部分内存导致的,那我们直接禁止掉 kdump服务。
Centos7禁止kdump
systemctl stop kdump.service #禁止服务 systemctl disable kdump.service #禁止开机启动
修改grub文件
vi /etc/default/grub
打开后可以看到类似如下的内容,将crashkernel改为0M即可,crashkernel=2G-8G:256M,8G-16G:512M,16G-:768M 这一段。改成crashkernel=0
GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL="serial console" GRUB_TERMINAL_OUTPUT="serial console" GRUB_CMDLINE_LINUX="crashkernel=2G-8G:256M,8G-16G:512M,16G-:768M console=ttyS0,115200 console=tty0 panic=5 net.ifnames=0 biosdevname=0 intel_idle.max_cstate=1 intel_pstate=disable processor.max_cstate=1 amd_iommu=on iommu=pt" GRUB_DISABLE_RECOVERY="true" GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
修改好后,执行
grub2-mkconfig -o /boot/grub2/grub.cfg
重启系统,重启之后,执行free -m 发现少掉的内存又回来了。总共找回来3950-3694=256MB内存。
free -m total used free shared buff/cache available Mem: 3950 503 3072 0 374 3225 Swap: 3999 0 3999
内存的厂家一般是按照1G=1000MB来算,计算机是二进制,1G=1024MB,所以有一点点误差是可以理解的。