前端之家收集整理的这篇文章主要介绍了
PostgreSQL的内存管理策略(1),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
作为一个大的系统软件,如果避免内存泄露(memory leakage)是系统实现所面对的关键问题之一,Postgre
sql利用MemoryContext这个概念来
解决这个问题,先来看看几个数据结构的定义:
注:在Postgre
sql的
代码中,一般struct会以名字
加上后缀“Data”,而该struct的指针就是该名字。
typedef@H_403_6@
struct MemoryContextMethods@H_403_6@
{
void@H_403_6@ *(*alloc) (MemoryContext context,Size size);@H_403_6@
/* call this free_p in case someone #define's free() */@H_403_6@@H_403_6@
void@H_403_6@ (*free_p) (MemoryContext context,void@H_403_6@ *pointer);@H_403_6@
void@H_403_6@ *(*realloc) (MemoryContext context,void@H_403_6@ *pointer,Size size);@H_403_6@
void@H_403_6@ (*init) (MemoryContext context);@H_403_6@
void@H_403_6@ (*reset) (MemoryContext context);@H_403_6@
void@H_403_6@ (*delete@H_403_6@) (MemoryContext context);@H_403_6@
Size (*get_chunk_space) (MemoryContext context,void@H_403_6@ *pointer);@H_403_6@
bool@H_403_6@ (*is_empty) (MemoryContext context);@H_403_6@
void@H_403_6@ (*stats) (MemoryContext context);@H_403_6@
#ifdef@H_403_6@ MEMORY_CONTEXT_CHECKING
void@H_403_6@ (*check) (MemoryContext context);@H_403_6@
} MemoryContextMethods;
typedef@H_403_6@
struct MemoryContextData@H_403_6@
{
NodeTag type; /* identifies exact kind of context */@H_403_6@@H_403_6@
MemoryContextMethods *methods; /* virtual function table */@H_403_6@@H_403_6@
MemoryContext parent; /* NULL if no parent (toplevel context) */@H_403_6@@H_403_6@
MemoryContext firstchild; /* head of linked list of children */@H_403_6@@H_403_6@
MemoryContext nextchild; /* next child of same parent */@H_403_6@@H_403_6@
char@H_403_6@ *name; /* context name (just for debugging) */@H_403_6@@H_403_6@
} MemoryContextData;
Postgre
sql对它的注释是“
A logical context in which memory allocations occur”,即它是一个内存分配的逻辑上下文,类似于C++中的虚拟类,对它有很多的实现方法,目前在Postgresql中,只有AllocSetContext@H_403_6@一种。从MemoryContextData的结构我们很容易看到,它是把每个内存上下文组织成一棵树,每个内存上下文有一个名字和一系列内存空间操作方法,这一整套操作方法的申明是在结构@H_403_6@MemoryContextMethods@H_403_6@中给出。@H_403_6@