我的服务端之内存池


分类: 我的服务端 C/C++   1836人阅读  评论(9)  收藏  举报
内存池(Memory Pool)
一、前言
1、操作系统的内存分配方式
1.1、连续分配方式
顾名思义,这种分配方式,会将进程分配在连续的空间。
连续分配方式一般可以分为固定分配方式、动态分配方式和伙伴系统(固定分配方式与动态分配方式的折衷方案)。
1.2、基本分页存储管理方式
1.3、基本分段存储管理方式
注:以上说的分配方式,自个可以到网上去搜索一下,方便理解以下内容。

二、为什么要添加内存池?内存池到底有什么作用?
1、避免内存碎片化。
1.1、什么是内存碎片?
内存碎片就是系统中程序频繁分配内存,会留下许多难以利用、很小的空闲分区,这些小分区被称为“零头”或“碎片”。
1.2、内存碎片的危害。
1.2.1、造成内存的浪费。
这是毫无疑问的。如下图所示。

当第一次分配6KB、6KB和8KB之后,蓝色部分的内被程序释放,系统收回。在之后又被另一个绿色程序分配了4KB。绿色与紫色之间只剩下2KB,当其它程序需要分
配大于2KB时候,剩下的空间不足够,这就成了碎片,浪费内存。极端的情况下会耗尽所有内存(这个情况很少会出现,毕竟现在的内存是白菜价)

1.2.2、降低内存的分配效率

一般来说操作系统都是查找空闲分区表或链表来分配空间的。就像之前说的一样,剩下2KB的碎片(假设不能再被分配),系统每次分配内存的时候都会来判断这
2KB能不能被分配。因此降低了内存的分配效率。

1.3、为什么能避免内存碎片化?
添加内存池,由于内存池会在初始化的时候分配一定长度的空间。因此内存池分配出来的内存结构很可能会像第一次分配出来的结果一样(红、蓝、紫)。而且一
般情况下程序运行过程中,内存池的内存都不会释放,直到程序结束。因此,内存池能很好地避免内存碎片。

2、提高内存分配与释放效率

有了上面的了解,我们知道无论系统的内存分配算法有多么地快速,也是不可能比我们从池中取出来内存快。

在服务端中,C++分配堆内存大小一般是类的长度(new出一个新对象)。因此下面讲解一下定长、列表的内存池。

  1. #ifndef MEMORYPOOL_H  
  2. #define MEMORYPOOL_H  
  3.   
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6.   
  7. struct MemoryList  
  8. {  
  9.     void * memory;  
  10.     MemoryList * next;  
  11. };  
  12.   
  13.   
  14. class MemoryPool  
  15. {  
  16. public:  
  17.     MemoryPool(unsigned int size, unsigned int increase = 64);  
  18.     ~MemoryPool();  
  19.     void *  Alloc();  
  20.     void    Free(void *m);  
  21.   
  22. private:  
  23.     unsigned int m_size;  
  24.     unsigned int m_increase;  
  25.     MemoryList * m_memory_list_header;  
  26.     MemoryList * m_handle;  
  27. };  
  28.   
  29.   
  30. #define REGISTER_MEMORYPOOL(PoolNameSpace, ClassName, IncreaseNum) \  
  31.     namespace PoolNameSpace\  
  32. {\  
  33.     MemoryPool g_##ClassName##_mem_pool(sizeof(ClassName), IncreaseNum);\  
  34. }\  
  35.     void *ClassName::operator new(size_t size)\  
  36. {\  
  37.     void *mem = PoolNameSpace::g_##ClassName##_mem_pool.Alloc();\  
  38.     return mem;\  
  39. }\  
  40.     void ClassName::operator delete(void *m)\  
  41. {\  
  42.     PoolNameSpace::g_##ClassName##_mem_pool.Free(m);\  
  43. }  
  44.   
  45. #endif  
memorypool.cpp
  1. #include "memorypool.h"  
  2.   
  3.   
  4. MemoryPool::MemoryPool( unsigned int size, unsigned int increase /*= 64*/ )  
  5. {  
  6.     m_size = size;  
  7.     m_increase = increase;  
  8.     MemoryList *list    = (MemoryList*)malloc(sizeof(MemoryList));  
  9.     m_handle = (MemoryList*)malloc(sizeof(MemoryList));  
  10.     if (list == NULL || m_handle == NULL)  
  11.     {  
  12.         return;  
  13.     }  
  14.     list->memory = malloc(size);  
  15.     m_memory_list_header = list;  
  16.     MemoryList *handle  = NULL;  
  17.     for (unsigned int i = 1; i < m_increase * 2; ++i)  
  18.     {  
  19.         handle = (MemoryList*)malloc(sizeof(MemoryList));  
  20.         handle->memory = malloc(size);  
  21.         list->next = handle;  
  22.         list = list->next;  
  23.     }  
  24.     list->next = NULL;  
  25. }  
  26.   
  27. MemoryPool::~MemoryPool()  
  28. {  
  29.     MemoryList *handle = NULL;  
  30.     while(m_memory_list_header->next != NULL)  
  31.     {  
  32.         handle = m_memory_list_header;  
  33.         m_memory_list_header = m_memory_list_header->next;  
  34.         free(handle);  
  35.     }  
  36. }  
  37.   
  38.   
  39.   
  40. void * MemoryPool::Alloc()  
  41. {  
  42.     static void * handle = NULL;  
  43.     if (m_memory_list_header->next == NULL)  
  44.     {  
  45.         // ÖØзÖÅäÄÚ´æ½øÀ´  
  46.         MemoryList *handle  = NULL;  
  47.         MemoryList *list    = (MemoryList*)malloc(sizeof(MemoryList));  
  48.         if (list == NULL)  
  49.         {  
  50.             return NULL;  
  51.         }  
  52.         list->memory = malloc(m_size);  
  53.         m_memory_list_header->next = list;  
  54.         for (unsigned int i = 1; i < m_increase ; ++i)  
  55.         {  
  56.             handle = (MemoryList*)malloc(sizeof(MemoryList));  
  57.             if (handle == NULL)  
  58.             {  
  59.                 break;  
  60.             }  
  61.             handle->memory = malloc(m_size);  
  62.             list->next = handle;  
  63.             list = list->next;  
  64.         }  
  65.         list->next = NULL;  
  66.     }  
  67.     handle = m_memory_list_header->memory;  
  68.     m_memory_list_header = m_memory_list_header->next;  
  69.     return handle;  
  70. }  
  71.   
  72. void MemoryPool::Free(void *m)  
  73. {  
  74.     if (m == NULL)  
  75.     {  
  76.         return ;  
  77.     }  
  78.     if ( m_handle == NULL)  
  79.     {  
  80.         m_handle = (MemoryList*)malloc(sizeof(MemoryList));  
  81.     }  
  82.     m_handle->memory = m;  
  83.     m_handle->next = m_memory_list_header;  
  84.     m_memory_list_header = m_handle;  
  85. }  
memorypoolconfig.cpp

  1. #include "memorypool.h"  
  2. #include "test.h"  
  3.   
  4. REGISTER_MEMORYPOOL(gamememorypool, Test, 64)  
Test 类
  1. #ifndef TEST_H  
  2. #define TEST_H  
  3.   
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6. #include <string.h>  
  7.   
  8. class Test  
  9. {  
  10. public:  
  11.     Test(){}  
  12.     ~Test(){}  
  13.     void Show();  
  14.     void Init();  
  15.   
  16.     void *  operator new(size_t size);  
  17.     void    operator delete(void *m);  
  18. private:  
  19.     int a;  
  20.     float b;  
  21.     char c;  
  22.     double d;  
  23.     char * e;  
  24. };  
  25.   
  26. #endif  
  27.   
  28. void Test::Init()  
  29. {  
  30.     a = 1;  
  31.     b = 2;  
  32.     c = 3;  
  33.     d = 4;  
  34.     e = (char *)malloc(16 * sizeof(char));  
  35.     memcpy(e, "一头汗"sizeof("一头汗"));  
  36. }  
  37.   
  38. void Test::Show()  
  39. {  
  40.     printf("%d\n",a);  
  41.     printf("%f\n",b);  
  42.     printf("%c\n",c);  
  43.     printf("%f\n",d);  
  44.     printf("%s\n",e);  
  45. }  
下面是测试用例

  1. #include <stdio.h>  
  2. #include <time.h>  
  3.   
  4. #include "globalvariable.h"  
  5. #include "luaengine.h"  
  6. #include "gamesocket.h"  
  7. #include "log.h"  
  8. #include "dll.h"  
  9. #include "MyDll.h"  
  10. #include "gametime.h"  
  11. #include "frame.h"  
  12. #include "datatable.h"  
  13. #include "showcrash.h"  
  14. #include "globalfunction.h"  
  15. #include "commonconfig.h"  
  16. #include "scene/areamanager.h"  
  17. #include "memorypool/test.h"  
  18.   
  19. class Test1  
  20. {  
  21. public:  
  22.     Test1(){}  
  23.     ~Test1(){}  
  24.     void Show();  
  25.     void Init();  
  26. private:  
  27.     int a;  
  28.     float b;  
  29.     char c;  
  30.     double d;  
  31.     char * e;  
  32. };  
  33.   
  34. void Test1::Init()  
  35. {  
  36.     a = 1;  
  37.     b = 2;  
  38.     c = 3;  
  39.     d = 4;  
  40.     e = (char *)malloc(16 * sizeof(char));  
  41.     memcpy(e, "一头汗"sizeof("一头汗"));  
  42. }  
  43.   
  44. void Test1::Show()  
  45. {  
  46.     printf("%d\n",a);  
  47.     printf("%f\n",b);  
  48.     printf("%c\n",c);  
  49.     printf("%f\n",d);  
  50.     printf("%s\n",e);  
  51. }  
  52.   
  53. #define TESTNUM 100000000  
  54. int main()  
  55. {  
  56.     clock_t start;  
  57.     start = clock();  
  58.     for (int i = 0; i < TESTNUM; ++i)  
  59.     {  
  60.          Test *t = new Test;  
  61.          delete t;  
  62.     }  
  63.     printf("use pool = %dms\n",(clock() - start)/1000);  
  64.     start = clock();  
  65.     for (int i = 0; i < TESTNUM; ++i)  
  66.     {  
  67.         Test1 *t = new Test1;  
  68.         delete t;  
  69.     }  
  70.      printf("normal = %dms\n",(clock() - start)/1000);  
  71.     return 0;  
  72. }  
输出结果:

Test类与Test1类不同的是,Test在memorypoolconfig.cpp重载了new/delete。
上面的代码为了方便排版,做了调整,如果有问题可以及时通知我。
如果上面的代码有错误,或者您有更好的方法都可以与我讨论!
交流群:315249378
欢迎交流与讨论!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值