STL的内存分配

1. 概述

STL Allocator是STL的内存管理器,也是最低调的部分之一,你可能使用了3年stl,但却不知其为何物。

STL标准如下介绍Allocator

the STL includes some low-level mechanisms for allocating and deallocating memory.Allocators are very specialized, and you can safely ignore them for almost all purposes.Allocators encapsulate allocation and deallocation of memory. They provide a low-level interface that permits efficient allocation of many small objects;different allocator types represent different schemes for memory management.

<STL 源码剖析>将其描述为空间配置器,理由是allocator可以将其它存储介质(例如硬盘)做为stl 容器的存储空间。由于内存是allocator管理的主要部分,因此,本文以STL内存管理为出发点介绍allocator。

Allocator就在我们身边,通常使用STL的方式:
#include <vector>
std::vector<int> Array(100);

本质上,调用的是:

#include <vector>
std::vector<int, std::allocator> Array(100);

std::allocator就是一个简单的Allocator

2. 为什么需要了解Allocator

项目中遇到的两个case
1)memory高位

线上使用vector保存待处理的数据,在停服务追数据的过程中,由于vector::clear并不释放内存,造成内存始终处于高位。
参考:http://blog.csdn.net/yfkiss/article/details/6537234

2)多线程使用vector、map

使用map保存状态,在多线程环境下,出现性能问题

在实际使用STL的过程中,会遇到很多问题,需要了解STL Allocator

3. 使用

针对不同的应用场合,STL中实现了不同的Allocator,如下(gcc-3.4:http://www.cs.huji.ac.il/~etsman/Docs/gcc-3.4-base/libstdc++/html/20_util/allocator.html):

__gnu_cxx::new_allocator<T> Simply wraps ::operator new and ::operator delete.
__gnu_cxx::malloc_allocator<T> Simply wraps malloc and free. There is also a hook for an out-of-memory handler
__gnu_cxx::debug_allocator<T> A wrapper around an arbitrary allocator A. It passes on slightly increased size requests to A, and uses the extra memory to store size information.
__gnu_cxx::__pool_alloc<bool, int> A high-performance, single pool allocator. The reusable memory is shared among identical instantiations of this type.
__gnu_cxx::__mt_alloc<T> A high-performance fixed-size allocatorthat was initially developed specifically to suit the needs of multi threaded applications
__gnu_cxx::bitmap_allocato A high-performance allocator that uses a bit-map to keep track of the used and unused memory locations

例如,在多线程环境下,可以使用:

 

[html] view plaincopy

  1. #include <vector> 
  2. #include <mt_allocator.h> 
  3. std::vector<int, __gnu_cxx::__mt_alloc<int>> Array(100); 
 
  1. #include <vector>

  2. #include <mt_allocator.h>

  3. std::vector<int, __gnu_cxx::__mt_alloc<int>> Array(100);

 

4.一个简单的Allocator实现
我们可以实现自己的allocator

[cpp] view plaincopy

  1. #include <memory> 
  2.  
  3. template<class T> 
  4. class my_allocator : public std::allocator<T> 
  5. public
  6. typedef std::allocator<T> base_type; 
  7.  
  8. // 必须要重新定义 
  9. template<class Other> 
  10. struct rebind 
  11. typedef my_allocator<Other> other; 
  12. }; 
  13. // 内存的分配与释放可以实现为自定义的算法 
  14. pointer allocate(size_type count) 
  15. {  
  16. return (base_type::allocate(count)); 
  17.  
  18. void deallocate(pointer ptr, size_type count) 
  19. {  
  20. base_type::deallocate(ptr, count); 
  21.  
  22.  
  23. // 构造函数 
  24. my_allocator() 
  25. {} 
  26.  
  27. my_allocator(my_allocator<T> const&) 
  28. {} 
  29.  
  30. my_allocator<T>& operator=(my_allocator<T> const&) 
  31. {  
  32. return (*this); 
  33.  
  34. template<class Other> 
  35. my_allocator(my_allocator<Other> const&) 
  36. {} 
  37.  
  38. template<class Other> 
  39. my_allocator<T>& operator=(my_allocator<Other> const&) 
  40. {  
  41. return (*this); } 
  42. };  
 
  1. #include <memory>

  2.  
  3. template<class T>

  4. class my_allocator : public std::allocator<T>

  5. {

  6. public:

  7. typedef std::allocator<T> base_type;

  8.  
  9. // 必须要重新定义

  10. template<class Other>

  11. struct rebind

  12. {

  13. typedef my_allocator<Other> other;

  14. };

  15. // 内存的分配与释放可以实现为自定义的算法

  16. pointer allocate(size_type count)

  17. {

  18. return (base_type::allocate(count));

  19. }

  20.  
  21. void deallocate(pointer ptr, size_type count)

  22. {

  23. base_type::deallocate(ptr, count);

  24. }

  25.  
  26.  
  27. // 构造函数

  28. my_allocator()

  29. {}

  30.  
  31. my_allocator(my_allocator<T> const&)

  32. {}

  33.  
  34. my_allocator<T>& operator=(my_allocator<T> const&)

  35. {

  36. return (*this);

  37. }

  38.  
  39. template<class Other>

  40. my_allocator(my_allocator<Other> const&)

  41. {}

  42.  
  43. template<class Other>

  44. my_allocator<T>& operator=(my_allocator<Other> const&)

  45. {

  46. return (*this); }

  47. };

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值