“存储器碎片"指的是“存储器碎片".还是一个问题吗? [英] "Memory Fragmentation" is it still an issue?

查看:191
本文介绍了“存储器碎片"指的是“存储器碎片".还是一个问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑.在操作系统课程中,我们被告知所有操作系统都通过分页或分段来处理内存碎片,并且根本没有连续的物理内存分配. OS使用不同级别的寻址(逻辑/物理)来避免连续的内存分配.现在此处有如此多的讨论.我的问题是: 这个问题在支持逻辑寻址的OS的C ++编程中是否存在(是否仅由于内存碎片而导致任何进程崩溃)?如果是,为什么每个操作系统都首先尝试避免连续寻址?

I'm a little bit confused. In the OS course we were told that all OSes take care of memory fragmentation by paging or segmentation and there is no contiguous physical memory allocation at all. OS uses different levels of addressing (logical/physical) to avoid the contiguous memory allocation. Now here there are so many discussions about it. My question is: Is this problem real in c++ programming for OSes supporting logical addressing (does any process crash just because of memory fragmentation)? if yes, why in the first place each OS tries to avoid contiguous addressing?

推荐答案

分为两层:虚拟进程地址空间中的碎片和物理内存中的碎片.

There are 2 layers: fragmentation in the virtual process address space and fragmentation in the physical memory.

如果您查看任何现代应用程序,您会看到它的内存使用率随着时间的推移如何增长,因为内存没有释放到操作系统.您可以说这是由其他原因引起的,但是内存碎片(例如,分配的内存块的不连续位置)是造成此问题的主要原因.简而言之,内存分配器拒绝将内存释放给操作系统.

If you look at any modern application, you can see how its memory usage grows over time as memory is not released to the OS. You can say this is caused by other things, but memory fragmentation (e.g. non-contiguous location of allocated memory chunks) is the core reason for this. In short, memory allocators refuse to release memory to the OS.

如果您对物理内存中的碎片感兴趣,那么即使将内存组织在页面中,仍然需要分配物理上连续的内存块.例如,如果您需要避免虚拟内存开销,则可能要使用大页面(在Linux中为大页面"). x86_64支持4KiB,2MiB和1GiB页面.如果没有所需大小的连续物理内存,则将无法使用它们.

If you are interested about fragmentation in physical memroy, then even with memory organized in pages, there is still a need to allocate physically contiguous memory chunks. For example if you need to avoid virtual memory overhead, you might want to use large pages ("huge pages" in terms of Linux). x86_64 supports 4KiB, 2MiB and 1GiB pages. If there is no contiguous physical memory of the required size, you won't be able to use them.

如果在操作系统中您的意思是内核",则它不能帮助您解决进程地址空间中发生的碎片(堆碎片). C库应尽量避免碎片化,不幸的是,它并非总是能够这样做.请参阅链接的问题.

If by OS you mean "kernel", then it cannot help you with fragmentation that happens in process address space (heap fragmentation). C library should try to avoid fragmentation, unfortunately, it is not always able to do so. See the linked question.

如果内存分配器中至少分配了某些内容,则通常无法释放大块内存.有一种解决方案,可以利用页面中的虚拟内存组织功能-由 DiscardVirtualMemory 在Windows上.当您有大量的内存仅被部分使用时,您可以通知内核不再需要该内存的一部分,并且可以在内存压力下将其收回.这是懒惰的,并且仅在内存压力下完成,因为内存重新分配非常昂贵.但是出于性能原因,许多内存分配器仍然不使用它.

Memory allocator is usually not able to release large chunk of memory if there is at least something allocated in it. There is a partial solution to this that takes advantage of virtual memory organization in pages - so called "lazy-free" mechanism represented by MADV_FREE on Linux and BSDs and DiscardVirtualMemory on Windows. When you have a huge chunk of memory that is only partially used, you can notify the kernel that part of that memory is not needed anymore and that it can take it back under memory pressure. This is done lazily and only under memory pressure because memory deallocation is extremely expensive. But many memory allocators still do not use it for performance reasons.

因此,问题的答案-取决于您对程序效率的关心程度.大多数程序都不在乎,因为标准分配器只是为它们完成工作.当标准分配器无法有效完成其工作时,某些程序可能会受到影响.

So the answer to your question - it depends on how much you care about efficiency of your program. Most program do not care, as standard allocator just does the job for them. Some programs might suffer when standard allocator is not able to do its job efficiently.

这篇关于“存储器碎片"指的是“存储器碎片".还是一个问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆