PostgreSQL的内存管理策略(1)

作为一个大的系统软件,如果避免内存泄露(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中给出。

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...