利用C/C++开发大型应用程序中,内存的管理与分配是一个须要认真考虑的部分。 程序员
本文描述了内存池设计原理并给出内存池的实现代码,代码支持Windows和Linux,多线程安全。 windows
内存池设计过程当中须要考虑好内存的分配与释放问题,其实也就是空间和时间的矛盾。 安全
有的内存池设计得很巧妙,内存分配与需求至关,可是会浪费过多的时间去查找分配与释放,这就得不偿失; 服务器
实际使用中,咱们更多的是关心内存分配的速度,而不是内存的使用效率。基于此,本文按照以下思想设计实现内存池。 多线程
主要包含三个结构:StiaticMemory, MemoryChunk和MemoryBlock,三者之间的关系以下图所示: 函数
1.内存的分配: 学习
(1)若是分配大小超过1024,直接采用malloc分配,分配的时候多分配sizeof(size_t)字节,用于保存该块的大小; 测试
(2)不然根据分配大小,查找到容纳该大小的最小size的MemoryChunk; this
(3)查找MemoryChunk的链表指针pList,找到空闲的MemoryBlock返回; spa
(4)若是pList为NULL,临时建立MemoryBlock返回;
(5)MemoryBlock头部包含两个成员,pChunk指向的所属的MemoryChunk对象,size代表大小,其后才是给用户使用的空间;
2.内存的释放:
(1)根据释放的指针,查找器size头部,即减去sizeof(size_t)字节,判断该块的大小;
(2)若是大小超过1024,直接free;
(3)不然交给MemoryChunk处理,而块的头部保存了该指针,所以直接利用该指针就能够收回该内存。
注意的问题:
上述设计的内存池经过冗余的头部来实现内存块的分配与释放,减小了内存池的操做时间,速度上要优于原始的malloc和free操做,同时减小了内存碎片的增长。
可是该设计中没有去验证释放的块冗余头部的正确性,所以故意释放不属于内存池中的块或者修改头部信息都会致使内存池操做失败,固然这些能够由程序员来控制。
此外,内存池中分配出去的内存块若是不主动释放,内存池没有保留信息,不会自动释放,可是在退出的时候会验证验证是否彻底释放,其实这个在系统测试时候就能够检测出来,我想这个缺陷也是能够弥补的,在此提出,但愿使用者注意。
下面贴上源码,若是对代码有任何建议或者发现存在的Bug,但愿与我联系,共同窗习交流,Tx。
MemoryChunk.h 文件,线程安全
#ifndef MEMORY_CHUNK_H
#define MEMORY_CHUNK_H
#include <cstdio>
#include <cassert>
#include <cstdlib>
#ifdef WIN32
#include <windows.h>
typedef CRITICAL_SECTION MUTEXTYPE;
#define INITMUTEX(hMutex) InitializeCriticalSection(&hMutex)
#define DELMUTEX(hMutex) DeleteCriticalSection(&hMutex)
#define LOCK(hMutex) EnterCriticalSection(&hMutex)
#define UNLOCK(hMutex) LeaveCriticalSection(&hMutex)
#else
#include <pthread.h>
typedef pthread_mutex_t MUTEXTYPE;
#define INITMUTEX(hMutex) pthread_mutex_init(&hMutex,NULL)
#define DELMUTEX(hMutex) pthread_mutex_destroy(&hMutex)
#define LOCK(hMutex) pthread_mutex_lock(&hMutex)
#define UNLOCK(hMutex) pthread_mutex_unlock(&hMutex)
#endif
class MemoryChunk;
/** @struct MemoryBlock
*
*/
struct BlockHeader
{
MemoryChunk* pChunk;
size_t len;
};
struct MemoryBlock;
struct BlockData
{
union{
MemoryBlock* pNext;
char pBuffer;
};
};
struct MemoryBlock
{
BlockHeader header;
BlockData data;
};
/** @class MemoryChunk
*
*/
class MemoryChunk
{
public:
MemoryChunk(size_t size, int count)
{
INITMUTEX(hMutex);
this->pFreeList=NULL;
this->size=size;
this->count=0;
MemoryBlock* pBlock;
while(count--){
pBlock=CreateBlock();
if(!pBlock)break;
pBlock->data.pNext=pFreeList;
pFreeList=pBlock;
}
}
~MemoryChunk()
{
int tempcount=0;
MemoryBlock* pBlock;
while(pBlock=pFreeList){
pFreeList=pBlock->data.pNext;
DeleteBlock(pBlock);
++tempcount;
}
assert(tempcount==count);//!确保释放彻底
DELMUTEX(hMutex);
}
void* malloc()
{
MemoryBlock* pBlock;
LOCK(hMutex);
if(pFreeList){
pBlock=pFreeList;
pFreeList=pBlock->data.pNext;
}
else{
if(!(pBlock=CreateBlock())){
UNLOCK(hMutex);
return NULL;
}
}
UNLOCK(hMutex);
return &pBlock->data.pBuffer;
}
static void free(void* pMem)
{
MemoryBlock* pBlock=(MemoryBlock*)((char*)pMem-sizeof(BlockHeader));
pBlock->header.pChunk->free(pBlock);
}
void free(MemoryBlock* pBlock)
{
LOCK(hMutex);
pBlock->data.pNext=pFreeList;
pFreeList=pBlock;
UNLOCK(hMutex);
}
MemoryChunk* Next(){return pNext;}
protected:
MemoryBlock* CreateBlock()
{
MemoryBlock* pBlock=(MemoryBlock*)::malloc(sizeof(BlockHeader)+size);
if(pBlock){
pBlock->header.pChunk=this;
pBlock->header.len=size;
++count;
}
return pBlock;
}
void DeleteBlock(MemoryBlock* pBlock)
{
::free(pBlock);
}
private:
MemoryBlock* pFreeList;
size_t size;//!Block大小
int count;//!Block数目
MemoryChunk* pNext;
MUTEXTYPE hMutex;
};
#endif
StaticMemory.h文件,内存池对象
#ifndef STATIC_MEMORY_H
#define STATIC_MEMORY_H
#include "MemoryChunk.h"
/** @ StaticMemory.h
* 定义实现内存池
* 采用固定大小策略进行内存管理与分配
* 减小因大量小内存分配致使的内存碎片增长
*/
struct HeapHeader
{
size_t size;
};
struct MemoryHeap
{
HeapHeader header;
char pBuffer;
};
class StaticMemory
{
public:
typedef enum{MAX_SIZE=1024,MIN_SIZE=sizeof(MemoryChunk*)};
StaticMemory()
{
chunkcount=0;
for(size_t size=MIN_SIZE; size<=MAX_SIZE; size*=2)++chunkcount;
//pChunkList=(MemoryChunk**)malloc(sizeof(MemoryChunk*)*chunkcount);
pChunkList=new MemoryChunk*[chunkcount];
int index=0;
for(size_t size=MIN_SIZE; size<=MAX_SIZE; size*=2)
{
pChunkList[index++]=new MemoryChunk(size,1000);
}
}
~StaticMemory()
{
for(int index=0; index<chunkcount; ++index)
{
delete pChunkList[index];
}
//free(pChunkList);
delete[] pChunkList;
}
void* Malloc(size_t size)
{
if(size>MAX_SIZE){
return malloc(size);
}
int index=0;
for(size_t tsize=MIN_SIZE; tsize<=MAX_SIZE; tsize*=2){
if(tsize>=size)break;
++index;
}
return pChunkList[index]->malloc();
}
void Free(void* pMem)
{
if(!free(pMem))MemoryChunk::free(pMem);
}
protected:
void* malloc(size_t size)
{
MemoryHeap* pHeap=(MemoryHeap*)::malloc(sizeof(HeapHeader)+size);
if(pHeap){
pHeap->header.size=size;
return &pHeap->pBuffer;
}
return NULL;
}
bool free(void* pMem)
{
MemoryHeap* pHeap=(MemoryHeap*)((char*)pMem-sizeof(HeapHeader));
if(pHeap->header.size>MAX_SIZE){
::free(pHeap);
return true;
}
return false;
}
private:
MemoryChunk** pChunkList;
int chunkcount;
};
#endif
ObejctManager.h文件,用于实现对象的建立与管理,比较简易。
#ifndef OBJECT_MANAGER_H
#define OBJECT_MANAGER_H
#include "StaticMemory.h"
/** @class ObjectManager
* 实现利用内存池建立对象
* 要求对象具备缺省构造函数
*/
template<typename T>
class ObjectManager
{
public:
typedef T ObjectType;
static ObjectType* Create(StaticMemory* pool)
{
void* pobject=pool->Malloc(sizeof(T));
new(pobject) ObjectType();
return static_cast<ObjectType*>(pobject);
}
static void Delete(StaticMemory* pool, ObjectType* pobject)
{
pobject->~ObjectType();
pool->Free(pobject);
}
};
#endif
测试结果:
分单线程和多线程进行测试,重复的内存分配与释放在实际使用中是不太可能的,为了模拟实际使用,经过随机数来肯定分配内存大小,同时也经过随机数来肯定分配与释放操做。在测试过程当中限制最大分配大小为1024,目的是为了测试小内存块的分配状况对比。
内存池单线程测试结果
分配与释放次数 |
malloc/free |
内存池 |
100,000 |
0.01s |
0.01s |
1,000,000 |
0.15s |
0.11s |
10,000,000 |
1.26s |
0.60s |
100,000,000 |
9.21s |
5.99s |
1,000,000,000 |
92.70s |
61.46s |
内存池多线程测试结果
线程数目 |
malloc/free |
内存池 |
1/1,000,000 |
0.15s |
0.10s |
2/1,000,000 |
1.49s |
0.73s |
4/1,000,000 |
9.945s |
6.71s |
8/1,000,000 |
45.60s |
28.82s |
进行多线程测试主要是测试多线程运行下,加锁给内存分配带来的影响,所以为了排除CPU的影响,测试采用的机器为16盒,16G内存的Linux服务器。
具体配置以下:
Intel(R) Xeon(R) CPU E5630 @ 2.53GHz
stepping : 2
cpu MHz : 2527.084
cache size : 12288 KB