什么是reserved memory size = 0


reserved memory size = 0是指在内核开启了CONFIG_MMU选项的情况下,表示内核预留的内存大小为0。

一、reserved memory size = 0的影响

如果reserved memory size = 0,会导致系统无法处理内存碎片,也无法将磁盘中的数据成功读入内存中。

在使用某些应用程序时,可能会由于缺少空闲内存而无法启动应用程序或者造成系统崩溃。

二、如何检测reserved memory size是否为0

在bootlog中可以查看reserved memory size。

$ dmesg | grep -i "reserved memory"
[ 0.000000] Reserved memory: initialized node linux,cma, compatible_id shared-dma-pool
[ 0.000000] Reserved memory: created CMA memory pool at 0x00000000dc000000, size 256 MiB

三、如何增加reserved memory size

可以通过修改内核的启动参数以增加reserved memory size。

$ vi /boot/grub/grub.cfg
在“linux”行的末尾添加“cma=128M”,表示预留128MB的内存空间。
linux /vmlinuz-4.15.0-1032-aws root=/dev/xvda1 ro console=tty1 console=ttyS0 nvme_core.io_timeout=4294967295 cma=128M

然后重新启动系统即可。

四、如何减小reserved memory size

可以通过修改内核的启动参数以减小reserved memory size。

$ vi /boot/grub/grub.cfg
在“linux”行的末尾添加“cma=32M”,表示预留32MB的内存空间。
linux /vmlinuz-4.15.0-1032-aws root=/dev/xvda1 ro console=tty1 console=ttyS0 nvme_core.io_timeout=4294967295 cma=32M

然后重新启动系统即可。

五、代码示例

#include 
#include 

/* Define a constant to control the number of bytes allocated. */
#define N_BYTES_ALLOCATED (10 * 1024 * 1024)

int main(void)
{
    void *p;
    int i;

    /* Allocate memory using malloc. */
    p = malloc(N_BYTES_ALLOCATED);
    if (p == NULL) {
        fprintf(stderr, "Failed to allocate memory.\n");
        return EXIT_FAILURE;
    }

    /* Assign values to the memory to force the memory to be allocated. */
    for (i = 0; i < N_BYTES_ALLOCATED; i++) {
        ((char *)p)[i] = i;
    }

    /* Free the memory. */
    free(p);

    printf("Done.\n");
    return EXIT_SUCCESS;
}

评论关闭