为什么取消分配堆内存比分配堆内存要慢得多? [英] Why deallocating heap memory is much slower than allocating it?

查看:77
本文介绍了为什么取消分配堆内存比分配堆内存要慢得多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个经验假设(分配要快于取消分配).

This is an empirical assumption (that allocating is faster then de-allocating).

这也是一个原因,我想是为什么基于堆的存储(例如 STL 容器或其他)选择不将当前未使用的内存返回给系统的原因(这就是适合缩小习惯用法诞生的原因.

This is also one of the reason, i guess, why heap based storages (like STL containers or else) choose to not return currently unused memory to the system (that is why shrink-to-fit idiom was born).

我们当然不应该将' heap '内存与类似' heap '的数据结构混淆.

And we shouldn't confuse, of course, 'heap' memory with the 'heap'-like data structures.

所以为什么取消分配速度较慢?

Windows 特定的(我在 Win 8.1 上看到它)还是 OS 独立?

Is it Windows-specific (i see it on Win 8.1) or OS independent?

是否有一些C ++特定的内存管理器自动参与使用' new '/' delete '或整个内存.管理是完全依靠OS的吗?(我知道 C ++ 11 引入了一些垃圾收集支持,我从未真正使用过,更好地依赖于旧的 stack static 持续时间或自我管理的容器 RAII ).

Is there some C++ specific memory manager automatically involved on using 'new' / 'delete' or the whole mem. management is completely relies on the OS? (i know C++11 introduced some garbage-collection support, which i never used really, better relying on the old stack and static duration or self managed containers and RAII).

此外,在 FOLLY字符串的代码中,我看到使用旧的C堆分配/取消分配,它比C ++的 new '/'删除"快吗?'?

Also, in the code of the FOLLY string i saw using old C heap allocation / deallocation, is it faster then C++ 'new' / 'delete'?

P.S.,请注意,有关虚拟内存机制的问题不是不是,我知道用户空间程序未使用真正的记忆.致敬.

P. S. please note that the question is not about virtual memory mechanics, i understand that user-space programs didn't use real mem. addresation.

推荐答案

我和@Basile有很多相同的想法:我想知道您的基本假设是否实际上(甚至接近)正确.由于您标记了C ++问题,因此我改用C ++编写了一个快速基准测试.

I had much the same idea as @Basile: I wondered whether your base assumption was actually (even close to) correct. Since you tagged the question C++, I wrote a quick benchmark in C++ instead.

#include <vector>
#include <iostream>
#include <numeric>
#include <chrono>
#include <iomanip>
#include <locale>

int main() {
    std::cout.imbue(std::locale(""));

    using namespace std::chrono;
    using factor = microseconds;

    auto const size = 2000;

    std::vector<int *> allocs(size);

    auto start = high_resolution_clock::now();

    for (int i = 0; i < size; i++)
        allocs[i] = new int[size];

    auto stop = high_resolution_clock::now();
    auto alloc_time = duration_cast<factor>(stop - start).count();

    start = high_resolution_clock::now();

    for (int i = 0; i < size; i++)
        delete[] allocs[i];

    stop = high_resolution_clock::now();

    auto del_time = duration_cast<factor>(stop - start).count();

    std::cout << std::left << std::setw(20) << "alloc time: " << alloc_time << " uS\n";
    std::cout << std::left << std::setw(20) << "del time: " << del_time << " uS\n";
}

我还在Windows上使用VC ++,而不是Linux上的gcc.但是结果并没有太大不同:释放内存所花的时间比分配它所花的时间少得多.这是三个连续运行的结果.

I also used VC++ on Windows instead of gcc on Linux. The result wasn't much different though: freeing the memory took substantially less time than allocating it did. Here are the results from three successive runs.

alloc time:         2,381 uS
del time:           1,429 uS

alloc time:         2,764 uS
del time:           1,592 uS

alloc time:         2,492 uS
del time:           1,442 uS

但是,我警告说,分配和释放(主要)是由标准库处理的,因此,一个标准库与另一个标准库(即使使用相同的编译器)也可能有所不同.我还要指出,如果在多线程代码中进行一些更改,这也不会令我感到惊讶.尽管实际上并不正确,但是似乎有些作者误解为在多线程环境中进行释放需要锁定堆以进行独占访问.可以避免这种情况,但是这样做的方法并不一定立即显而易见.

I'd warn, however, allocation and freeing is handled (primarily) by the standard library, so this could be different between one standard library and another (even when using the same compiler). I'd also note that it wouldn't surprise me if this were to change somewhat in multi-threaded code. Although it's not actually correct, there appear to be a few authors who are under the mis-apprehension that freeing in a multithreaded environment requires locking a heap for exclusive access. This can be avoided, but the means to do so isn't necessarily immediately obvious.

这篇关于为什么取消分配堆内存比分配堆内存要慢得多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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