什么是内存碎片? [英] What is memory fragmentation?

查看:264
本文介绍了什么是内存碎片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过内存碎片化这个术语在C ++动态内存分配的上下文中使用了几次。我发现了一些关于如何处理内存碎片的问题,但是找不到一个处理它本身的直接问题。所以:

I've heard the term "memory fragmentation" used a few times in the context of C++ dynamic memory allocation. I've found some questions about how to deal with memory fragmentation, but can't find a direct question that deals with it itself. So:


  • 什么是内存碎片?

  • 如何判断内存碎片是否存在问题为我的应用程序?

  • 处理内存碎片的常见方法是什么?

还有:


  • 我听说过使用动态分配很多可能会增加内存碎片。这是真的?在C ++的上下文中,我理解所有的标准容器(std :: string,std :: vector等)使用动态内存分配。如果这些在整个程序中使用(特别是std :: string),内存碎片更可能是一个问题?

  • 如何在STL重的应用程序中处理内存碎片?

推荐答案

想象一下,你有一个大(32字节)

Imagine that you have a "large" (32 bytes) expanse of free memory:

----------------------------------
|                                |
----------------------------------

现在,分配一些(5个分配):

Now, allocate some of it (5 allocations):

----------------------------------
|aaaabbccccccddeeee              |
----------------------------------

现在,释放前四个分配,而不是第五个:

Now, free the first four allocations but not the fifth:

----------------------------------
|              eeee              |
----------------------------------

现在,尝试分配16个字节。糟糕,我不能,即使有近两倍的自由。

Now, try to allocate 16 bytes. Oops, I can't, even though there's nearly double that much free.

在具有虚拟内存的系统上,碎片不是一个你可能认为的问题,因为大分配只需要在虚拟地址空间中是连续的,而不是在物理地址空间中。所以在我的例子中,如果我有一个页面大小为2字节的虚拟内存,那么我可以使我的16字节分配没有问题。物理内存如下所示:

On systems with virtual memory, fragmentation is less of a problem than you might think, because large allocations only need to be contiguous in virtual address space, not in physical address space. So in my example, if I had virtual memory with a page size of 2 bytes then I could make my 16 byte allocation with no problem. Physical memory would look like this:

----------------------------------
|ffffffffffffffeeeeff            |
----------------------------------

而虚拟内存(更大)可能如下所示:

whereas virtual memory (being much bigger) could look like this:

------------------------------------------------------...
|              eeeeffffffffffffffff                   
------------------------------------------------------...

内存碎片的典型症状是尝试分配一个大块,你不能,即使你似乎有足够的内存空间。另一个可能的后果是进程无法释放内存回到操作系统(因为有一些对象仍然使用在它从操作系统分配的所有块中,即使那些块现在大多没有使用)。

The classic symptom of memory fragmentation is that you try to allocate a large block and you can't, even though you appear to have enough memory free. Another possible consequence is the inability of the process to release memory back to the OS (because there's some object still in use in all the blocks it has allocated from the OS, even though those blocks are now mostly unused).

在C ++中防止内存碎片的策略通过根据其大小和/或预期寿命从不同区域分配对象来工作。因此,如果你要创建很多对象,并在以后将它们全部一起销毁,请从内存池中分配它们。你在他们之间做的任何其他分配都不会从池中,因此不会位于它们之间的内存中,所以内存不会被分割的结果。

Tactics to prevent memory fragmentation in C++ work by allocating objects from different areas according to their size and/or their expected lifetime. So if you're going to create a lot of objects and destroy them all together later, allocate them from a memory pool. Any other allocations you do in between them won't be from the pool, hence won't be located in between them in memory, so memory will not be fragmented as a result.

一般来说,你不需要担心它,除非你的程序是长期运行,并做了大量的分配和释放。这是当你有短命和长寿命的对象的混合物,你最危险,但即使 malloc 将尽力帮助。基本上,忽略它,直到你的程序有分配失败或意外地导致系统运行低内存(在测试中,优先考虑。)

Generally you don't need to worry about it much, unless your program is long-running and does a lot of allocation and freeing. It's when you have mixtures of short-lived and long-lived objects that you're most at risk, but even then malloc will do its best to help. Basically, ignore it until your program has allocation failures or unexpectedly causes the system to run low on memory (catch this in testing, for preference!).

标准库没有任何比分配内存的任何东西,标准容器都有一个 Alloc 模板参数,如果绝对必要,您可以使用它来微调其分配策略。

The standard libraries are no worse than anything else that allocates memory, and standard containers all have an Alloc template parameter which you could use to fine-tune their allocation strategy if absolutely necessary.

这篇关于什么是内存碎片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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