PostgreSQL的内存管理策略(1)
发布时间:2020-12-13 18:03:06 所属栏目:百科 来源:网络整理
导读:作为一个大的系统软件,如果避免内存泄露(memory leakage)是系统实现所面对的关键问题之一,PostgreSQL利用MemoryContext这个概念来解决这个问题,先来看看几个数据结构的定义: 注:在PostgreSQL的代码中,一般struct会以名字加上后缀“Data”,而该struc
作为一个大的系统软件,如果避免内存泄露(memory leakage)是系统实现所面对的关键问题之一,PostgreSQL利用MemoryContext这个概念来解决这个问题,先来看看几个数据结构的定义:
注:在PostgreSQL的代码中,一般struct会以名字加上后缀“Data”,而该struct的指针就是该名字。
typedef
struct MemoryContextMethods
{
void *(*alloc) (MemoryContext context,Size size);
/* call this free_p in case someone #define's free() */
void (*free_p) (MemoryContext context,void *pointer);
void *(*realloc) (MemoryContext context,void *pointer,Size size);
void (*init) (MemoryContext context);
void (*reset) (MemoryContext context);
void (*delete) (MemoryContext context);
Size (*get_chunk_space) (MemoryContext context,void *pointer);
bool (*is_empty) (MemoryContext context);
void (*stats) (MemoryContext context);
#ifdef MEMORY_CONTEXT_CHECKING
void (*check) (MemoryContext context);
#endif
} MemoryContextMethods;
typedef
struct MemoryContextData
{
NodeTag type; /* identifies exact kind of context */
MemoryContextMethods *methods; /* virtual function table */
MemoryContext parent; /* NULL if no parent (toplevel context) */
MemoryContext firstchild; /* head of linked list of children */
MemoryContext nextchild; /* next child of same parent */
char *name; /* context name (just for debugging) */
} MemoryContextData;
PostgreSQL对它的注释是“
A logical context in which memory allocations occur”,即它是一个内存分配的逻辑上下文,类似于C++中的虚拟类,对它有很多的实现方法,目前在PostgreSQL中,只有AllocSetContext一种。从MemoryContextData的结构我们很容易看到,它是把每个内存上下文组织成一棵树,每个内存上下文有一个名字和一系列内存空间操作方法,这一整套操作方法的申明是在结构MemoryContextMethods中给出。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |