会在mmap文件上进行分类以减少内存消耗吗?

时间:2019-07-19 08:18:49

标签: linux file filesystems mmap fallocate

我试图将64M文件映射到内存中,然后读写到其中。但是有时此文件中的某个范围将不再使用,因此我叫fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 16 << 20, 16 << 20);在其中释放16M范围。

但是,我发现内存消耗似乎没有改变(从free -mcat /proc/meminfo都改变了。)

我知道,fallocate会在文件内部挖出一些漏洞,并将这种范围返回给文件系统。但是我不确定是否会减少映射文件的内存消耗。

如果不减少内存消耗,该范围在哪里?另一个进程可以获取先前分配给它的基础内存吗?

big.file是普通的64M文件,而不是稀疏文件

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <linux/falloc.h>
#include <stdint.h>

int main(int argc, char *argv[])
{
        uint8_t *addr;

        int fd = open("home/big.file", O_RDWR | O_CREAT, 0777);
        if (fd < 0)
                return -1;

        addr = (uint8_t *)mmap(NULL, MMAP_SIZE , PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
        if (addr < 0)
                return -1;

        printf("data[0x%x] = %d\n", offset, addr[offset]);
        getchar();
        fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 16 << 20, 16 << 20);
        getchar();

        close(fd);
        return 0;
}

0 个答案:

没有答案
相关问题