C++: Memory allocators

前端 未结 5 1155
情话喂你
情话喂你 2020-12-25 15:51

I\'ve heard about people using custom memory allocators for their project, particulary in C++.

  • What is a custom memory allocator, compared to malloc?

5条回答
  •  温柔的废话
    2020-12-25 16:12

    There's an extensive description of custom allocators, along with their empirical evaluation, in the following paper (that I co-wrote). Before you decide to use custom allocators in your C++ project, you should give this paper a read. The executive overview is a good general-purpose allocator is better (faster and more space-efficient) than all styles of custom allocators except regions, but these have serious problems.

    Reconsidering Custom Memory Allocation (ACM link, direct PDF link, Powerpoint talk slides), OOPSLA 2002.

    Programmers hoping to achieve performance improvements often use custom memory allocators. This in-depth study examines eight applications that use custom allocators. Surprisingly, for six of these applications, a state-of-the-art general-purpose allocator (the Lea allocator) performs as well as or better than the custom allocators. The two exceptions use regions, which deliver higher performance (improvements of up to 44%). Regions also reduce programmer burden and eliminate a source of memory leaks. However, we show that the inability of programmers to free individual objects within regions can lead to a substantial increase in memory consumption. Worse, this limitation precludes the use of regions for common programming idioms, reducing their usefulness.We present a generalization of general-purpose and region-based allocators that we call reaps. Reaps are a combination of regions and heaps, providing a full range of region semantics with the addition of individual object deletion. We show that our implementation of reaps provides high performance, outperforming other allocators with region-like semantics. We then use a case study to demonstrate the space advantages and software engineering benefits of reaps in practice. Our results indicate that programmers needing fast regions should use reaps, and that most programmers considering custom allocators should instead use the Lea allocator.

提交回复
热议问题