6.单线程内存池
1. 频繁地分配和回收内存会严重影响性能,通过开发专用的内存管理器可以解决这个问题。
2. 对专用内存管理器的设计至少要从两个方面考虑:大小和并发。
从大小的角度分为以下两种:
(1)、固定大小:分配固定大小内存块的内存管理器。
(2)、可变大小:分配任意大小内存块的内存管理器。所请求分配的大小事先是未知的。
从并发的角度也分为以下两种:
(1)、单线程:内存管理器局限在一个线程内。内存被一个线程使用,并且不越出该线程的界限。这种内存管理器不涉及相互访问的多线程。
(2)、多线程:内存管理器被多个线程并发地使用。这种实现需要包含互斥执行的代码段。无论什么时候,只能有一个线程在执行一个代码段。
案例:
- 版本0:全局函数new()和delete()
#include <iostream>
#include <windows.h>
static LARGE_INTEGER cpuFreq;
static LARGE_INTEGER startTime;
static LARGE_INTEGER endTime;
static double run_time = 0.0;
using namespace std;
class Rational0 //一百万次运行:73.3ms
{
public:
Rational0(int a = 0, int b = 1) : n(a),d(b) {}
private:
int n; // 分子
int d; // 分母
};
int main()
{
Rational0 *array0[1000];
QueryPerformanceFrequency(&cpuFreq); //获取系统时钟的频率
QueryPerformanceCounter(&startTime);
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
array0[j] = new Rational0(j);
}
for (int k = 0; k < 1000; k++) {
delete array0[k];
}
}
QueryPerformanceCounter(&endTime);
run_time = (((endTime.QuadPart - startTime.QuadPart) * 1000.0) / cpuFreq.QuadPart);
cout << "run_time: "<< run_time <<"ms" << endl;
Rational2::deleteMemPool();
while(1);
}
- 版本1:专用内存管理器
![3d7bbabbbec29c15a873fa528f7d2852.png](https://i-blog.csdnimg.cn/blog_migrate/b8f3c38a57c588876793c3be993413f2.png)
![6b1661634b638608e39fcb3c755f4261.png](https://i-blog.csdnimg.cn/blog_migrate/f0625da0695cdd2a885b64d36cb72df9.png)
#include <iostream>
#include <windows.h>
static LARGE_INTEGER cpuFreq;
static LARGE_INTEGER startTime;
static LARGE_INTEGER endTime;
static double run_time = 0.0;
using namespace std;
class NextOnFreeList {
//声明一个辅助结构来连接空闲列表的相邻元素
public:
NextOnFreeList* next;
};
/*需求:避免频繁使用默认内存管理器,原理是先分配指定大小的内存空间,当空间使用完时再分配*/
class Rational1 {
//一百万次运行:9.03ms
public:
Rational1(int a = 0, int b = 1) : n(a),d(b) {}
//Rational2的操作符new()和delete()用来管理静态列表。重载全局的new()和delete()。
inline void* operator new(size_t size);
inline void operator delete(void* doomed);
static void newMemPool() {
expandTheFreeList(); }
static void deleteMemPool();
private:
static NextOnFreeList* freeList; //空闲静态列表
static void expandTheFreeList(); //静态成员函数,避免从新生成一个类变量,可以节省内存,方便调用,但不能访问类中的非静态成员
enum {
EXPANSION_SIZE = 32};
int n; // 分子
int d; // 分母
};
NextOnFreeList* Rational1::freeList = nullptr;
inline void* Rational1::operator new(size_t size)
{
if(nullptr == freeList){
expandTheFreeList(); //列表为空时,扩展
}
NextOnFreeList *head = freeList;
freeList = head->next;
return head;
}
inline void Rational1::operator delete(void* doomed)
{
NextOnFreeList *head = static_cast<NextOnFreeList *>(doomed);
head->next = freeList;
freeList = head;
}
void Rational1::expandTheFreeList() //
{
//必须分配足够大的对象以包含下一个指针
size_t size = (sizeof (Rational1) > sizeof (NextOnFreeList *)) ? sizeof (Rational1) : sizeof (NextOnFreeList *);
NextOnFreeList* runner = static_cast<NextOnFreeList*>((void*)new char[size]);
freeList = runner;
for (int i = 0; i < EXPANSION_SIZE; ++i) {
//修改runner的地址同时也修改了freeList,因为是指针类型,并将runner->next强制转换成NextOnFreeList*类型
runner->next = static_cast<NextOnFreeList*>((void*)new char[size]);
runner = runner->next;
}
runner->next = nullptr;
}
void Rational1::deleteMemPool()
{
NextOnFreeList* nextPtr;
for (nextPtr = freeList; nextPtr != nullptr; nextPtr = freeList) {
freeList = freeList->next;
delete [] nextPtr;
}
}
int main()
{
Rational1 *array1[1000];
Rational1::newMemPool();
QueryPerformanceFrequency(&cpuFreq); //获取系统时钟的频率
QueryPerformanceCounter(&startTime);
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
array1[j] = new Rational1(j);
}
for (int k = 0; k < 1000; k++) {
delete array1[k];
}
}
QueryPerformanceCounter(&endTime);
run_time = (((endTime.QuadPart - startTime.QuadPart) * 1000.0) / cpuFreq.QuadPart);
cout << "run_time: "<< run_time <<"ms" << endl;
Rational1::deleteMemPool();
while(1);
}
- 版本2:固定大小对象的内存池
#include <iostream>
#include <windows.h>
static LARGE_INTEGER cpuFreq;
static LARGE_INTEGER startTime;
static LARGE_INTEGER endTime;
static double run_time = 0.0;