简体   繁体   English

'当一个大块被“锁定”在较小的块之间'时,mmap 不起作用是什么意思?

[英]What does it mean that mmap doesn't work 'when a large chunk becomes “locked” in between smaller ones'?

I was reading the GNU C library document for the GNU memory allocator.我正在阅读 GNU memory 分配器的 GNU C 库文档。 https://www.gnu.org/software/libc/manual/html_node/The-GNU-Allocator.html https://www.gnu.org/software/libc/manual/html_node/The-GNU-Allocator.html

In the paragraph about using mmap to allocate big chunks of memory, it says在关于使用 mmap 分配大块 memory 的段落中,它说

The other way of memory allocation is for very large blocks, ie much larger than a page. memory 分配的另一种方式是用于非常大的块,即比页面大得多。 These requests are allocated with mmap (anonymous or via /dev/zero; see Memory-mapped I/O)).这些请求使用 mmap 分配(匿名或通过 /dev/zero;请参阅内存映射 I/O))。 This has the great advantage that these chunks are returned to the system immediately when they are freed.这具有很大的优势,即这些块在被释放时会立即返回到系统。

Therefore, it cannot happen that a large chunk becomes “locked” in between smaller ones and even after calling free wastes memory.因此,即使在调用免费浪费 memory 之后,大块也不会“锁定”在较小块之间。

From what I've understood mmap seems to be a function where I can assign a contiguous memory space to a file (or vice versa?).据我了解, mmap 似乎是一个 function ,我可以在其中为文件分配一个连续的 memory 空间(反之亦然?)。

So I don't understand this part, can't the memory chunk be freed even though it is between smaller ones?所以我不明白这部分,memory 块即使在较小的块之间也不能被释放吗? And then we can use it for mmap?然后我们可以将它用于mmap吗?

What kind of situation would this be in a real program?这在实际程序中会是什么样的情况?

Consider a long-running logical function of a program that allocates lots and lots of memory.考虑一个长期运行的逻辑 function 程序,它分配大量的 memory。 And at the same time that logical function is running, other bits of code allocate very small chunks of memory.并且在逻辑 function 运行的同时,其他代码位分配非常小的 memory 块。

Later on, that long-running function may finish and the massive amount of memory it allocated is all freed.稍后,长时间运行的 function 可能会完成,并且它分配的大量 memory 将全部释放。 But lots of small bits allocated by other logical functions of the program are still holding memory.但是程序的其他逻辑函数分配的许多小位仍然持有memory。

You may have a case where the program requested a huge amount of memory from the operating system and almost all of that memory is free, but nevertheless, no memory can be returned to the operating system by the program's allocator because every chunk it allocated from the system still has a tiny piece in use.您可能会遇到这样的情况,即程序向操作系统请求了大量的 memory,几乎所有的 memory 都是免费的,但是,没有 memory 可以返回到程序分配的每个操作系统 allocator系统仍有一小部分在使用。 This is called "memory fragmentation".这称为“内存碎片”。

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

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