简体   繁体   English

为什么我的 Linux 没有执行惰性 memory 分配?

[英]Why did NOT my Linux act the lazy memory allocation?

I'm practicing to use the Lazy Allocation and the Demand Paging policies of Linux.我正在练习使用 Linux 的惰性分配和需求分页策略。

I want a buffer that I allocated by mmap() occupy NO physical memory until I really write something to it.我想要一个由mmap()分配的缓冲区占用NO物理 memory 直到我真正向它写入内容。 Further more, I want it gradually enlarge(use more physical memory) with a step size of the swap page size(eg 4K) of Linux along with I'm writing continuously from its head to the tail.此外,我希望它逐渐扩大(使用更多物理内存),步长为 Linux 的交换页面大小(例如 4K),同时我从头到尾连续写入。

According to some docs and searchings , it should NOT enlarge if there be only reading access on it, but the reality I observed in a experiment does NOT like this.根据一些文档搜索,如果只有读取权限,它应该放大,但我在实验中观察到的现实不喜欢这样。

To test this, I coded a program as following, and watched the memory status by top shell command when it running.为了测试这一点,我编写了一个程序如下,并在运行时通过top shell 命令观察 memory 状态。

constexpr size_t BUF_SIZE = 1024 * 1024 * 1024;

int main( int argc, char** argv ) {
    auto shm_pt = mmap( NULL, BUF_SIZE, PROT_READ | PROT_WRITE,
                        MAP_SHARED | MAP_ANONYMOUS, -1, 0 );
    if( shm_pt == MAP_FAILED ) {
        std::cerr << "mmap error:" << shm_pt;
        exit( EXIT_FAILURE );
    };

    bool full_zero = true;
    uint8_t* pc = reinterpret_cast<uint8_t*>( shm_pt );

    constexpr size_t STEP_SIZE = 1024 * 1024;
    for( size_t j = 0; j < BUF_SIZE / STEP_SIZE; ++j ) {
        this_thread::sleep_for( 100ms );
        size_t base = j * STEP_SIZE;
        std::cerr << "Reading from " << base / 1024 / 1024 << "M..." << endl;

        for( size_t i = 0; i < STEP_SIZE; ++i )
            full_zero = full_zero && pc[ base + i ] == 0;
    }

    if( !full_zero )
        std::cerr << "The buffer has not been initialized with full zeros!";

    for( size_t j = 0; j < BUF_SIZE / STEP_SIZE; ++j ) {
        this_thread::sleep_for( 100ms );
        size_t base = j * STEP_SIZE;
        std::cerr << "Writing to " << base / 1024 / 1024 << "M..." << endl;

        for( size_t i = 0; i < STEP_SIZE; ++i )
            pc[ base + i ] = 'c';
    }

    munmap( shm_pt, BUF_SIZE );
    return EXIT_SUCCESS;
};

What I observed is that the physical memory used by my app is growing gradually along with the Reading operation not with Writing op!我观察到的是,我的应用程序使用的物理 memory 随着读取操作而不是写入操作逐渐增长!

What is going wrong?出了什么问题? Maybe my comprehension is wrong?也许我的理解是错误的?

Any hints will be appreciated, Thanks!!!任何提示将不胜感激,谢谢!!!

I got it!我知道了!

In the searching content I pasted, that man used a MAP_PRIVATE flag to mmap() as argument, while I used MAP_SHARED .在我粘贴的搜索内容中,该人使用mmap()MAP_PRIVATE标志作为参数,而我使用MAP_SHARED

It looks like that if a buffer is being shared between processes, a READING operation also results real memory allocation!看起来如果在进程之间共享缓冲区,READING 操作也会导致真正的 memory 分配!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM